📖 Scenario: Imagine you run a small online store. You want to keep track of every order your customers make. To do this, you use Kafka, a system that helps send and store messages quickly and safely.
🎯 Goal: Learn why producers publish data to Kafka and how to create a simple producer that sends order messages.
📋 What You'll Learn
Create a Kafka topic named orders
Write a producer that sends order data to the orders topic
Understand the role of the producer in sending data
Print confirmation after sending each order
💡 Why This Matters
🌍 Real World
Online stores, banks, and many apps use Kafka producers to send data like orders, transactions, or logs quickly and reliably.
💼 Career
Understanding Kafka producers is important for roles in data engineering, backend development, and real-time data processing.
Progress0 / 4 steps
1
Create a list of orders
Create a list called orders with these exact dictionaries: {'order_id': 101, 'item': 'Book'}, {'order_id': 102, 'item': 'Pen'}, {'order_id': 103, 'item': 'Notebook'}
Kafka
Hint
Use a list with dictionaries inside. Each dictionary has keys 'order_id' and 'item'.
2
Set the Kafka topic name
Create a variable called topic_name and set it to the string 'orders'
Kafka
Hint
Just assign the string 'orders' to the variable topic_name.
3
Send orders to Kafka topic
Use a for loop with variable order to go through orders and inside the loop, call producer.send(topic_name, value=order)
Kafka
Hint
Use a for loop to send each order dictionary to Kafka using producer.send().
4
Print confirmation after sending
Inside the for loop, after sending, write print(f"Sent order {order['order_id']} to topic {topic_name}")
Kafka
Hint
Use a print statement inside the loop to show which order was sent and to which topic.
Practice
(1/5)
1. Why do Kafka producers publish data to topics?
easy
A. To delete old messages from Kafka topics
B. To store data permanently on the producer machine
C. To send messages so other applications can receive and process them
D. To create new Kafka topics automatically
Solution
Step 1: Understand the role of producers in Kafka
Producers send data to Kafka topics to share information with other applications.
Step 2: Identify the purpose of publishing data
Publishing allows other consumers to receive and process the messages in real time.
Final Answer:
To send messages so other applications can receive and process them -> Option C
Quick Check:
Producers publish data to share it [OK]
Hint: Producers send data for others to use it [OK]
Common Mistakes:
Thinking producers store data locally
Confusing producers with consumers
Believing producers delete messages
2. Which of the following is the correct way for a Kafka producer to send a message to a topic named orders?
easy
A. producer.send('orders', message)
B. producer.publish('orders', message)
C. producer.write('orders', message)
D. producer.push('orders', message)
Solution
Step 1: Recall Kafka producer API method
The Kafka producer uses the send method to publish messages to a topic.
Step 2: Match method to correct syntax
Only producer.send('orders', message) matches the correct Kafka producer syntax.
Final Answer:
producer.send('orders', message) -> Option A
Quick Check:
Use send() to publish messages [OK]
Hint: Use send() method to publish messages [OK]
Common Mistakes:
Using publish(), write(), or push() which are not Kafka methods
Confusing consumer methods with producer methods
Incorrect method names cause runtime errors
3. What will be the output if a Kafka producer sends a message to a topic but the topic does not exist and auto-creation is disabled?
medium
A. The message is sent successfully and stored
B. The producer automatically creates the topic and sends the message
C. The message is silently dropped without error
D. An error is thrown indicating the topic does not exist