0
0
Kafkadevops~10 mins

Why consumers process messages in Kafka - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to consume messages from a Kafka topic.

Kafka
from kafka import KafkaConsumer

consumer = KafkaConsumer('[1]', bootstrap_servers=['localhost:9092'])
for message in consumer:
    print(message.value)
Drag options to blanks, or click blank then click option'
Amy_topic
Bproducer
Cbroker
Dpartition
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'producer' or 'broker' instead of the topic name.
Leaving the topic name blank.
2fill in blank
medium

Complete the code to decode the message value as UTF-8 string.

Kafka
for message in consumer:
    text = message.value.[1]('utf-8')
    print(text)
Drag options to blanks, or click blank then click option'
Asplit
Bstrip
Cdecode
Dencode
Attempts:
3 left
💡 Hint
Common Mistakes
Using encode instead of decode.
Trying to print bytes directly without decoding.
3fill in blank
hard

Fix the error in the code to commit the message offset manually.

Kafka
for message in consumer:
    print(message.value.decode('utf-8'))
    consumer.[1]()
Drag options to blanks, or click blank then click option'
Acommit
Bcommit_async
Ccommit_offsets
Dcommit_sync
Attempts:
3 left
💡 Hint
Common Mistakes
Using the synchronous 'commit()' method.
Using 'commit_sync' which is not a valid method.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

Kafka
words = ['apple', 'bat', 'car', 'door']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Mapping word to word instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their values only if the value is greater than 0.

Kafka
data = {'apple': 5, 'bat': 0, 'car': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' instead of 'k.upper()' for keys.
Using '<' instead of '>' in the condition.