0
0
Kafkadevops~10 mins

Python producer (confluent-kafka) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Kafka producer using confluent-kafka.

Kafka
from confluent_kafka import Producer

conf = {'bootstrap.servers': [1]
producer = Producer(conf)
Drag options to blanks, or click blank then click option'
A'kafka:2181'
B'localhost:9092'
C'127.0.0.1:2181'
D'localhost:2181'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Zookeeper port 2181 instead of Kafka broker port 9092.
Using incorrect IP or hostname format.
2fill in blank
medium

Complete the code to send a message to the topic 'test-topic'.

Kafka
producer.produce([1], key='key1', value='Hello Kafka!')
producer.flush()
Drag options to blanks, or click blank then click option'
A'topic1'
B'my-topic'
C'kafka-topic'
D'test-topic'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong topic name.
Forgetting to call flush() to ensure delivery.
3fill in blank
hard

Fix the error in the delivery report callback function to print success or failure.

Kafka
def delivery_report(err, msg):
    if [1] is not None:
        print(f"Message delivery failed: {err}")
    else:
        print(f"Message delivered to {msg.topic()} [{msg.partition()}]")
Drag options to blanks, or click blank then click option'
Aerr
Bmsg
Cerror
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the wrong variable for error.
Using 'error' instead of 'err'.
4fill in blank
hard

Fill both blanks to produce messages with keys and values from a list.

Kafka
messages = [('key1', 'value1'), ('key2', 'value2')]
for [1], [2] in messages:
    producer.produce('my-topic', key=[1], value=[2])
producer.flush()
Drag options to blanks, or click blank then click option'
Akey
Bk
Cvalue
Dv
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names not matching the unpacked tuple.
Using the same variable name for both key and value.
5fill in blank
hard

Fill all three blanks to create a producer with a delivery callback and send a message.

Kafka
def delivery_report([1], [2]):
    if [1] is not None:
        print(f"Failed: {err}")
    else:
        print(f"Delivered to {msg.topic()}")

conf = {'bootstrap.servers': 'localhost:9092'}
producer = Producer(conf)
producer.produce('topic1', key='k1', value='v1', callback=[3])
producer.flush()
Drag options to blanks, or click blank then click option'
Aerr
Bmsg
Cdelivery_report
Dcallback
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names in the callback.
Passing a string instead of the function as callback.