📖 Scenario: You are building a simple Python program that sends messages to a Kafka topic. Kafka is like a post office where your program can send letters (messages) to be delivered to others.We will use the confluent-kafka library to create a producer that sends messages to a Kafka topic named test_topic.
🎯 Goal: Create a Python Kafka producer that sends three messages to the test_topic topic and prints confirmation for each message sent.
📋 What You'll Learn
Create a Kafka producer with the confluent-kafka library
Set the Kafka broker address to localhost:9092
Send three specific messages to the topic test_topic
Print confirmation after each message is sent
💡 Why This Matters
🌍 Real World
Kafka producers are used in real applications to send data streams like logs, user activity, or sensor data to Kafka topics for processing.
💼 Career
Understanding how to produce messages to Kafka is important for roles in data engineering, backend development, and real-time data processing.
Progress0 / 4 steps
1
Set up Kafka producer configuration
Create a dictionary called conf with the key 'bootstrap.servers' set to 'localhost:9092'.
Kafka
Hint
This dictionary tells the producer where the Kafka server is located.
2
Create the Kafka producer
Import Producer from confluent_kafka and create a producer object called producer using the conf dictionary.
Kafka
Hint
Use Producer(conf) to create the producer object.
3
Send messages to the Kafka topic
Use producer.produce() to send these messages to the topic 'test_topic': 'Hello Kafka', 'This is message 2', and 'Final message'. Use a for loop with variables msg and messages to iterate over the list of messages.
Kafka
Hint
Use producer.produce('test_topic', msg.encode('utf-8')) inside the loop to send each message.
4
Flush and print confirmation
Call producer.flush() to ensure all messages are sent. Then, use a for loop with variables msg and messages to print "Sent: {msg}" for each message.
Kafka
Hint
Use producer.flush() to wait for all messages to be sent before printing.
Practice
(1/5)
1. What is the main purpose of the produce() method in the Python confluent-kafka producer?
easy
A. To consume messages from a Kafka topic
B. To connect to the Kafka server
C. To send a message to a Kafka topic
D. To wait for all messages to be delivered
Solution
Step 1: Understand the role of produce()
The produce() method is used to send messages to a Kafka topic from the producer.
Step 2: Differentiate from other methods
flush() waits for messages to be sent, and connection is set by configuration, not produce().
Final Answer:
To send a message to a Kafka topic -> Option C
Quick Check:
produce() sends messages [OK]
Hint: Remember: produce() means send message [OK]
Common Mistakes:
Confusing produce() with flush()
Thinking produce() connects to Kafka
Mixing producer and consumer roles
2. Which of the following is the correct way to set the Kafka server address in the Python confluent-kafka producer configuration?
easy
A. "bootstrap.servers" : "localhost:9092"
B. "server.bootstrap" : "localhost:9092"
C. "kafka.server" : "localhost:9092"
D. "broker.address" : "localhost:9092"
Solution
Step 1: Recall the correct configuration key
The correct key to specify Kafka server address is bootstrap.servers.
Step 2: Match the option
Only "bootstrap.servers" : "localhost:9092" uses the exact key "bootstrap.servers" with a valid address format.
Final Answer:
"bootstrap.servers" : "localhost:9092" -> Option A
Quick Check:
Kafka server config = bootstrap.servers [OK]
Hint: Look for exact key: bootstrap.servers [OK]
Common Mistakes:
Swapping key names like server.bootstrap
Using incorrect keys like kafka.server
Missing quotes or wrong format
3. What will be the output of the following Python code using confluent-kafka producer?
from confluent_kafka import Producer
p = Producer({'bootstrap.servers': 'localhost:9092'})
p.produce('my_topic', key='key1', value='hello')
p.flush()
print('Message sent')
medium
A. Error: No Kafka server found
B. hello
C. No output
D. Message sent
Solution
Step 1: Understand the code flow
The code creates a producer, sends a message to 'my_topic', waits for delivery with flush(), then prints 'Message sent'.
Step 2: Identify the printed output
The only print statement outputs the string 'Message sent'. The message sending is asynchronous but flush ensures completion before print.
Final Answer:
Message sent -> Option D
Quick Check:
flush() waits, then print runs [OK]
Hint: flush() waits, print runs after sending [OK]
Common Mistakes:
Expecting message value to print
Ignoring flush() effect
Assuming error without Kafka running
4. Identify the error in this Python confluent-kafka producer code:
from confluent_kafka import Producer
p = Producer({'bootstrap.servers': 'localhost:9092'})
p.produce('my_topic', key='key1', 'hello')
p.flush()
medium
A. Missing key argument in produce()
B. Incorrect order of arguments in produce()
C. flush() called before produce()
D. Missing import statement
Solution
Step 1: Check produce() argument order
The produce() method expects positional arguments first (topic, optionally value), followed only by keyword arguments. Here, key='key1' (keyword) is followed by 'hello' (positional), causing SyntaxError: positional argument follows keyword argument.
Step 2: Identify correct usage
Correct usage: produce('my_topic', value='hello') or produce('my_topic', key='key1', value='hello').
Final Answer:
Incorrect order of arguments in produce() -> Option B
Quick Check:
Positional args before keywords [OK]
Hint: Positional args must precede keywords [OK]
Common Mistakes:
Placing positional argument after keyword argument
Mixing positional and keyword argument order
Forgetting Python positional-before-keyword rule
5. You want to send multiple messages to Kafka and ensure all are delivered before your program exits. Which sequence of calls is correct using confluent-kafka producer in Python?
hard
A. Call produce() for each message, then call flush() once at the end
B. Call flush() after each produce() call
C. Call flush() before any produce() calls
D. Call produce() once, then call flush() multiple times
Solution
Step 1: Understand message sending behavior
produce() queues messages asynchronously. Calling it multiple times queues multiple messages.
Step 2: Use flush() to wait for delivery
Calling flush() once after all produce() calls waits for all messages to be sent before program exit.
Final Answer:
Call produce() for each message, then call flush() once at the end -> Option A
Quick Check:
Produce many, flush once after all [OK]
Hint: Flush once after all produce calls to wait [OK]
Common Mistakes:
Flushing after every produce (slow)
Flushing before producing messages
Not flushing at all causing lost messages
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.