0
0
Kafkadevops~20 mins

Python producer (confluent-kafka) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Producer Mastery
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 the following Python code using confluent-kafka to produce messages. What will be printed when this code runs?

Kafka
from confluent_kafka import Producer

def delivery_report(err, msg):
    if err is not None:
        print(f"Message delivery failed: {err}")
    else:
        print(f"Message delivered to {msg.topic()} [{msg.partition()}] at offset {msg.offset()}")

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

p.produce('test_topic', key='key1', value='value1', callback=delivery_report)
p.flush()
ANo output printed
BMessage delivery failed: Broker transport failure
CMessage delivered to test_topic [0] at offset 0
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint

Think about what the delivery_report callback prints when the message is successfully sent.

Predict Output
intermediate
2:00remaining
What error does this Kafka producer code raise?

What error will this code raise when executed?

Kafka
from confluent_kafka import Producer

p = Producer({'bootstrap.servers': 'localhost:9092'})
p.produce('test_topic', value=12345)
p.flush()
ATypeError: value must be bytes or string
BValueError: topic name invalid
CNo error, message sent successfully
DKeyError: 'value'
Attempts:
2 left
💡 Hint

Check the type of the value argument expected by produce.

🔧 Debug
advanced
2:00remaining
Why does this Kafka producer code hang indefinitely?

This code hangs and never finishes. What is the cause?

Kafka
from confluent_kafka import Producer

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

for i in range(5):
    p.produce('test_topic', value=f'message {i}')

# Missing flush call here
AThe produce method is blocking and never returns
BThe topic name is invalid causing a hang
CThe loop causes an infinite loop
DThe producer buffers messages and needs <code>flush()</code> to send them
Attempts:
2 left
💡 Hint

Think about how the producer sends messages to Kafka brokers.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in Kafka producer code?

Which of these code snippets will cause a syntax error when defining the Kafka producer?

Ap = Producer({'bootstrap.servers' = 'localhost:9092'})
Bp = Producer({'bootstrap.servers': 'localhost:9092', 'enable.idempotence': True})
Cp = Producer({'bootstrap.servers': 'localhost:9092'})
Dp = Producer({'bootstrap.servers': 'localhost:9092', 'client.id': 'myclient'})
Attempts:
2 left
💡 Hint

Check the syntax for dictionary key-value pairs in Python.

🚀 Application
expert
3:00remaining
How many messages are successfully produced by this code?

Given this code snippet, how many messages will be successfully produced and delivered?

Kafka
from confluent_kafka import Producer

results = []

def delivery_report(err, msg):
    if err is None:
        results.append(msg.value().decode('utf-8'))

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

for i in range(3):
    p.produce('test_topic', value=f'msg{i}'.encode('utf-8'), callback=delivery_report)

p.flush()

print(len(results))
A1
B3
C0
DRaises a TypeError
Attempts:
2 left
💡 Hint

Consider how the callback updates the results list and when flush() is called.