Complete the code to create a Kafka producer that sends a message.
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers=[1]) producer.send('my_topic', b'Hello World') producer.flush()
The Kafka producer connects to the Kafka broker using the bootstrap server address, which usually runs on port 9092.
Complete the code to consume messages from a Kafka topic.
from kafka import KafkaConsumer consumer = KafkaConsumer('my_topic', bootstrap_servers=[1]) for message in consumer: print(message.value)
The Kafka consumer connects to the Kafka broker on port 9092 to receive messages.
Fix the error in the code to produce messages asynchronously.
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers='localhost:9092') future = producer.send('my_topic', [1]) result = future.get(timeout=10)
Kafka producer expects message data as bytes, so the message must be a bytes object like b'Hello Kafka'.
Fill both blanks to create a dictionary comprehension that filters events with value greater than 10.
events = {'a': 5, 'b': 15, 'c': 20}
filtered = {k: v for k, v in events.items() if v [1] 10 and k [2] 'b'}The comprehension keeps events where value is greater than 10 and key is not 'b'.
Fill all three blanks to create a dictionary comprehension that uppercases keys, keeps values, and filters values greater than 10.
events = {'a': 5, 'b': 15, 'c': 20}
result = {{ [1]: [2] for k, v in events.items() if v [3] 10 }}The comprehension creates a new dictionary with uppercase keys, original values, and filters values greater than 10.