Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to produce a message to the Kafka topic.
Kafka
producer.send('[1]', b'Hello Kafka!')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a topic name that is not defined or different from the example.
Forgetting to put the topic name as a string.
✗ Incorrect
The code sends a message to the Kafka topic named 'topic1'.
2fill in blank
mediumComplete the code to consume a message from the Kafka topic.
Kafka
for message in consumer.[1](): print(message.value.decode('utf-8'))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'subscribe' instead of 'consume' to get messages.
Using a method that does not exist on the consumer object.
✗ Incorrect
The consumer uses the consume() method to get messages from the topic.
3fill in blank
hardFix the error in the code to produce a message with the correct key.
Kafka
producer.send('topic1', key=[1], value=b'Hello')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string without b prefix for the key.
Using a variable name without quotes or bytes.
✗ Incorrect
The key must be bytes, so b'key1' is correct.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps message keys to values.
Kafka
messages = {msg.[1]: msg.[2] for msg in consumer.consume()} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'topic' or 'partition' instead of 'key' or 'value'.
Mixing up key and value positions.
✗ Incorrect
Each message's key and value are accessed by msg.key and msg.value.
5fill in blank
hardFill all three blanks to produce and flush a message to the topic with a key and value.
Kafka
producer.send('[1]', key=[2], value=[3]) producer.flush()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of bytes for key or value.
Forgetting to flush after sending.
✗ Incorrect
The message is sent to 'topic1' with key b'key123' and value b'Hello Kafka!'.