Complete the code to create a Kafka producer.
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers=[1])
The Kafka producer connects to the Kafka broker using the bootstrap server address, which usually runs on port 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. 'my_topic' is the example topic to send the message.
Fix the error in the consumer code to subscribe to a topic.
from kafka import KafkaConsumer consumer = KafkaConsumer([1], bootstrap_servers='localhost:9092')
The KafkaConsumer expects a list of topics to subscribe to, so the topic name must be inside a list.
Complete the code to create a dictionary comprehension that maps partition numbers to their offsets.
offsets = {partition: consumer.position(partition) [1] partition in consumer.assignment()}In dictionary comprehensions, the syntax is {key: value for item in iterable}. So ':' separates key and value, and 'for' starts the loop.
Fill both blanks to create a dictionary comprehension filtering partitions with offsets greater than 100.
filtered_offsets = {partition: offset [1] partition, offset [2] offsets.items() if offset > 100}The dictionary comprehension syntax is {key: value for key, value in dict.items() if condition}. So ':' separates key and value, 'for' starts the loop, and 'in' specifies the iterable.