0
0
Kafkadevops~10 mins

Why event-driven scales applications 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 create a Kafka producer that sends a message.

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

Complete the code to consume messages from a Kafka topic.

Kafka
from kafka import KafkaConsumer
consumer = KafkaConsumer('my_topic', bootstrap_servers=[1])
for message in consumer:
    print(message.value)
Drag options to blanks, or click blank then click option'
A'localhost:2181'
B'kafka:2181'
C'127.0.0.1:2181'
D'localhost:9092'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ZooKeeper port 2181 instead of Kafka broker port 9092.
Confusing broker and ZooKeeper addresses.
3fill in blank
hard

Fix the error in the code to produce messages asynchronously.

Kafka
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
future = producer.send('my_topic', [1])
result = future.get(timeout=10)
Drag options to blanks, or click blank then click option'
Ab'Hello Kafka'
B'Hello Kafka'
CHello Kafka
Db"Hello Kafka"
Attempts:
3 left
💡 Hint
Common Mistakes
Sending a string instead of bytes causes errors.
Forgetting the b prefix for bytes literals.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters events with value greater than 10.

Kafka
events = {'a': 5, 'b': 15, 'c': 20}
filtered = {k: v for k, v in events.items() if v [1] 10 and k [2] 'b'}
Drag options to blanks, or click blank then click option'
A>
B<
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > for value comparison.
Using == instead of != for key exclusion.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that uppercases keys, keeps values, and filters values greater than 10.

Kafka
events = {'a': 5, 'b': 15, 'c': 20}
result = {{ [1]: [2] for k, v in events.items() if v [3] 10 }}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using k.lower() instead of k.upper().
Using < instead of > for filtering.