Complete the code to send a message to a Kafka topic.
producer.send('[1]', b'Hello World')
The producer must specify the topic name where the data will be published. Here, my_topic is the correct topic.
Complete the code to create a Kafka producer with the correct bootstrap server.
producer = KafkaProducer(bootstrap_servers='[1]')
The bootstrap_servers parameter must point to the Kafka broker address, usually localhost:9092 for local setups.
Fix the error in the code to publish a message with a key.
producer.send('my_topic', key=[1], value=b'Hello')
The key must be bytes, so b'key1' is correct. Using a string without b'' causes an error.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.
{word: [1] for word in words if [2]The dictionary maps each word to its length using len(word). The condition filters words with length greater than 3 using len(word) > 3.
Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values only if value is positive.
result = [1]: [2] for [3], [2] in data.items() if [2] > 0
The keys are converted to uppercase with k.upper(). The values are v. The loop variables are k and v. The condition filters positive values.