Complete the code to create a Kafka producer.
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers=[1])
The bootstrap_servers parameter expects a string with host and port, like 'localhost:9092'.
Complete the code to send a message to a Kafka topic.
producer.send([1], b'Hello, Kafka!')
The send method requires the topic name as a string.
Fix the error in the Kafka consumer code to subscribe to a topic.
consumer = KafkaConsumer()
consumer.subscribe([1])The subscribe method expects a list of topic names.
Fill both blanks to create a cloud-native Kafka consumer that reads from a topic and auto-commits offsets.
consumer = KafkaConsumer([1], bootstrap_servers='localhost:9092', [2]=True)
The first argument is the topic name as a string. The parameter to enable auto commit is enable_auto_commit.
Fill all three blanks to produce a cloud-native Kafka producer that sends JSON messages with a key and flushes the buffer.
import json producer = KafkaProducer(bootstrap_servers='localhost:9092', value_serializer=lambda v: json.dumps(v).encode('utf-8')) producer.send([1], key=[2], value={'event': 'start'}) producer.[3]()
The topic name is 'events' as a string, the key must be bytes, and flush() sends all buffered messages.