Complete the code to consume messages from a Kafka topic.
from kafka import KafkaConsumer consumer = KafkaConsumer('[1]', bootstrap_servers=['localhost:9092']) for message in consumer: print(message.value)
The consumer must subscribe to the correct topic name to receive messages. Here, 'my_topic' is the topic name.
Complete the code to decode the message value as UTF-8 string.
for message in consumer: text = message.value.[1]('utf-8') print(text)
Message values are bytes and need to be decoded to strings using decode('utf-8').
Fix the error in the code to commit the message offset manually.
for message in consumer: print(message.value.decode('utf-8')) consumer.[1]()
To commit offsets manually in KafkaConsumer, use commit_async() for asynchronous commits.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ['apple', 'bat', 'car', 'door'] lengths = {word: [1] for word in words if len(word) [2] 3}
The dictionary comprehension maps each word to its length using len(word) and filters words with length greater than 3 using >.
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their values only if the value is greater than 0.
data = {'apple': 5, 'bat': 0, 'car': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }The comprehension maps uppercase keys k.upper() to their values v only if v > 0.