0
0
Kafkadevops~30 mins

Producer API basics in Kafka - Mini Project: Build & Apply

Choose your learning style9 modes available
Producer API basics
📖 Scenario: You are working on a simple messaging system where you need to send messages to a Kafka topic. This is common in real-world applications like logging systems, event tracking, or communication between services.
🎯 Goal: Build a Kafka producer that sends a few messages to a specific Kafka topic using the Producer API basics.
📋 What You'll Learn
Create a Kafka producer with the correct configuration
Send messages to a Kafka topic named test-topic
Use a loop to send multiple messages
Print confirmation after sending each message
💡 Why This Matters
🌍 Real World
Kafka producers are used in many real-world systems to send data streams like logs, user activity, or sensor data to other services for processing.
💼 Career
Understanding Kafka producer basics is essential for roles in backend development, data engineering, and real-time data processing.
Progress0 / 4 steps
1
Create Kafka producer configuration
Create a Properties object called props and set the following properties exactly: bootstrap.servers to localhost:9092, key.serializer to org.apache.kafka.common.serialization.StringSerializer, and value.serializer to org.apache.kafka.common.serialization.StringSerializer.
Kafka
Need a hint?

Use props.put(key, value) to set each configuration property.

2
Create Kafka producer instance
Create a KafkaProducer<String, String> object called producer using the props configuration.
Kafka
Need a hint?

Use new KafkaProducer<>(props) to create the producer.

3
Send messages to the topic
Use a for loop with an integer variable i from 1 to 3 inclusive. Inside the loop, send a message to the topic test-topic with the value "Message " + i using producer.send() and a ProducerRecord<String, String>. Use i as the key.
Kafka
Need a hint?

Use new ProducerRecord<>(topic, key, value) inside producer.send().

4
Print confirmation after sending
Inside the for loop, after sending each message, print "Sent message: Message " + i. After the loop, close the producer using producer.close().
Kafka
Need a hint?

Use System.out.println() inside the loop and producer.close() after the loop.