Complete the code to create a Kafka producer using confluent-kafka.
from confluent_kafka import Producer conf = {'bootstrap.servers': [1] producer = Producer(conf)
The bootstrap.servers config requires the Kafka broker address with port 9092, not the Zookeeper port 2181.
Complete the code to send a message to the topic 'test-topic'.
producer.produce([1], key='key1', value='Hello Kafka!') producer.flush()
flush() to ensure delivery.The message must be sent to the topic named test-topic as specified.
Fix the error in the delivery report callback function to print success or failure.
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()}]")
The callback receives err which is None on success, so checking if err is not None detects failure.
Fill both blanks to produce messages with keys and values from a list.
messages = [('key1', 'value1'), ('key2', 'value2')] for [1], [2] in messages: producer.produce('my-topic', key=[1], value=[2]) producer.flush()
In the loop, k and v are used as variable names for key and value respectively.
Fill all three blanks to create a producer with a delivery callback and send a message.
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()
The delivery callback function takes err and msg as parameters, and the callback argument is the function delivery_report.