How to Create a Producer in Kafka: Simple Guide
To create a Kafka producer, instantiate a
KafkaProducer object with configuration properties like bootstrap.servers and serializers. Then use the send() method to publish messages to a Kafka topic.Syntax
The basic syntax to create a Kafka producer involves setting configuration properties and creating a KafkaProducer instance. Key properties include bootstrap.servers (Kafka server addresses), key.serializer, and value.serializer which define how keys and values are converted to bytes.
After creating the producer, use send() to send messages asynchronously.
java
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer<String, String> producer = new KafkaProducer<>(props); ProducerRecord<String, String> record = new ProducerRecord<>("topic-name", "key", "value"); producer.send(record); producer.close();
Example
This example shows a complete Java program that creates a Kafka producer, sends a message to a topic named test-topic, and then closes the producer.
java
import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerRecord; import java.util.Properties; public class SimpleProducer { public static void main(String[] args) { Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer<String, String> producer = new KafkaProducer<>(props); ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "myKey", "Hello Kafka"); producer.send(record); System.out.println("Message sent successfully"); producer.close(); } }
Output
Message sent successfully
Common Pitfalls
- Not setting
bootstrap.serverscorrectly causes connection failures. - Using wrong serializers leads to serialization errors.
- Forgetting to close the producer can cause resource leaks.
- Not handling exceptions from
send()may hide message delivery failures.
Always verify Kafka server is running and the topic exists before sending messages.
java
/* Wrong: Missing serializer properties */ Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); // KafkaProducer<String, String> producer = new KafkaProducer<>(props); // This will throw an error /* Right: Include serializers */ props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer<String, String> producerCorrect = new KafkaProducer<>(props);
Quick Reference
Remember these key points when creating a Kafka producer:
- bootstrap.servers: Kafka broker addresses
- key.serializer & value.serializer: Convert data to bytes
- send(): Sends messages asynchronously
- close(): Releases resources
Key Takeaways
Set bootstrap.servers and serializers correctly to create a Kafka producer.
Use send() to publish messages asynchronously to a Kafka topic.
Always close the producer to free resources.
Handle exceptions to catch message delivery issues.
Verify Kafka broker and topic availability before sending messages.