0
0
Kafkadevops~10 mins

Message broker architecture 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 create a Kafka producer.

Kafka
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers=[1])
Drag options to blanks, or click blank then click option'
A'localhost:9092'
B'127.0.0.1:2181'
C'kafka:2181'
D'localhost:2181'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Zookeeper port 2181 instead of Kafka broker port 9092.
2fill in blank
medium

Complete the code to send a message to a Kafka topic.

Kafka
producer.send([1], b'Hello Kafka')
Drag options to blanks, or click blank then click option'
A'default'
B'topic1'
C'kafka_topic'
D'my_topic'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an incorrect or non-existent topic name.
3fill in blank
hard

Fix the error in the consumer code to subscribe to a topic.

Kafka
from kafka import KafkaConsumer
consumer = KafkaConsumer([1], bootstrap_servers='localhost:9092')
Drag options to blanks, or click blank then click option'
A'topic1'
B['my_topic']
C'my_topic'
D['topic1']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a list of strings for topics.
4fill in blank
hard

Complete the code to create a dictionary comprehension that maps partition numbers to their offsets.

Kafka
offsets = {partition: consumer.position(partition) [1] partition in consumer.assignment()}
Drag options to blanks, or click blank then click option'
A:
Bfor
Cin
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' for key-value separation.
Using 'in' instead of 'for' in comprehension.
5fill in blank
hard

Fill both blanks to create a dictionary comprehension filtering partitions with offsets greater than 100.

Kafka
filtered_offsets = {partition: offset [1] partition, offset [2] offsets.items() if offset > 100}
Drag options to blanks, or click blank then click option'
A:
Bfor
Cin
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' for key-value.
Using 'in' instead of 'for' or vice versa.