0
0
Kafkadevops~10 mins

Python consumer in Kafka - Interactive Code Practice

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

Complete the code to import the KafkaConsumer class from the kafka module.

Kafka
from kafka import [1]

consumer = KafkaConsumer('my_topic')
Drag options to blanks, or click blank then click option'
AProducer
BKafkaConsumer
CKafkaProducer
DConsumerGroup
Attempts:
3 left
💡 Hint
Common Mistakes
Importing KafkaProducer instead of KafkaConsumer
Using Producer instead of KafkaConsumer
2fill in blank
medium

Complete the code to create a KafkaConsumer that reads from the topic 'orders'.

Kafka
consumer = KafkaConsumer([1])
Drag options to blanks, or click blank then click option'
A'orders'
B'payments'
C'users'
D'logs'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong topic name
Forgetting to put quotes around the topic name
3fill in blank
hard

Fix the error in the code to correctly consume messages from Kafka.

Kafka
for message in consumer.[1]():
    print(message.value)
Drag options to blanks, or click blank then click option'
Apoll
Bconsume
Csend
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' which is for producers
Using 'subscribe' which subscribes to topics but does not fetch messages
4fill in blank
hard

Fill both blanks to create a consumer that reads from 'events' topic and uses 'localhost:9092' as the Kafka server.

Kafka
consumer = KafkaConsumer([1], bootstrap_servers=[2])
Drag options to blanks, or click blank then click option'
A'events'
B'localhost:9092'
C'127.0.0.1:9092'
D'my_topic'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong topic name
Using wrong server address format
5fill in blank
hard

Fill all three blanks to create a KafkaConsumer that reads from 'logs', connects to 'broker:9092', and uses 'earliest' offset reset.

Kafka
consumer = KafkaConsumer([1], bootstrap_servers=[2], auto_offset_reset=[3])
Drag options to blanks, or click blank then click option'
A'logs'
B'broker:9092'
C'earliest'
D'latest'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'latest' instead of 'earliest' for offset reset
Wrong server address format
Wrong topic name