0
0
Kafkadevops~20 mins

First message (produce and consume) in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka First Message Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kafka producer code?

Consider this Kafka producer code snippet in Python using confluent_kafka. What will it print?

Kafka
from confluent_kafka import Producer

conf = {'bootstrap.servers': 'localhost:9092'}
producer = Producer(conf)

producer.produce('test-topic', key='key1', value='Hello Kafka!')
producer.flush()

print('Message sent')
ATimeoutError
BMessage sent
CKeyError
DHello Kafka!
Attempts:
2 left
💡 Hint

The produce method queues the message, and flush sends it. The print statement runs after sending.

Predict Output
intermediate
2:00remaining
What does this Kafka consumer print after consuming one message?

Given this Kafka consumer code in Python, what will it print after receiving one message?

Kafka
from confluent_kafka import Consumer

conf = {
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'mygroup',
    'auto.offset.reset': 'earliest'
}
consumer = Consumer(conf)
consumer.subscribe(['test-topic'])

msg = consumer.poll(1.0)
if msg is None:
    print('No message received')
elif msg.error():
    print(f'Error: {msg.error()}')
else:
    print(f'Received: {msg.value().decode("utf-8")}')
consumer.close()
AError: KafkaError{code=_TIMED_OUT}
BNo message received
CReceived: key1
DReceived: Hello Kafka!
Attempts:
2 left
💡 Hint

The consumer reads the message value, decoding bytes to string.

🔧 Debug
advanced
3:00remaining
Why does this Kafka consumer code raise an error?

This Kafka consumer code raises an exception. What is the cause?

Kafka
from confluent_kafka import Consumer

conf = {
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'mygroup'
}
consumer = Consumer(conf)
consumer.subscribe(['test-topic'])

msg = consumer.poll(1.0)
print(msg.value().decode('utf-8'))
consumer.close()
ATimeoutError because poll timeout is too short
BKeyError because 'auto.offset.reset' is missing
CAttributeError because msg is None and msg.value() is called
DTypeError because decode is called on a string
Attempts:
2 left
💡 Hint

Check what happens if poll returns None.

📝 Syntax
advanced
2:00remaining
Which option correctly produces a message with key and value in Kafka?

Which of these Python code snippets correctly produces a Kafka message with key 'user1' and value 'data'?

Aproducer.produce('topic', key='user1', value='data')
Bproducer.produce('topic', 'user1', 'data')
Cproducer.produce(topic='topic', key='user1', value='data')
Dproducer.produce('topic', key=user1, value=data)
Attempts:
2 left
💡 Hint

Remember that key and value must be strings or bytes, and keyword arguments are required.

🚀 Application
expert
3:00remaining
How many messages will be consumed by this Kafka consumer loop?

Given this Kafka consumer code, how many messages will it consume before stopping?

Kafka
from confluent_kafka import Consumer

conf = {
    'bootstrap.servers': 'localhost:9092',
    'group.id': 'mygroup',
    'auto.offset.reset': 'earliest'
}
consumer = Consumer(conf)
consumer.subscribe(['test-topic'])

count = 0
while True:
    msg = consumer.poll(0.5)
    if msg is None:
        break
    if msg.error():
        break
    count += 1
consumer.close()
print(count)
ANumber of messages currently in 'test-topic' partition(s)
B0 because poll timeout is too short
CInfinite loop because break conditions are wrong
D1 because it stops after first message
Attempts:
2 left
💡 Hint

The loop stops when poll returns None, which means no more messages are available.