How to Send Message to Kafka: Simple Guide with Example
To send a message to
Kafka, create a KafkaProducer with the broker address and key/value serializers, then use the send() method with a ProducerRecord specifying the topic and message. Finally, close the producer to free resources.Syntax
The basic syntax to send a message to Kafka involves creating a KafkaProducer, preparing a ProducerRecord with the topic and message, and calling send() to publish the message.
- KafkaProducer: Connects to Kafka brokers and sends messages.
- ProducerRecord: Holds the topic name and the message (value) to send.
- send(): Sends the record asynchronously to Kafka.
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", "message-value"); producer.send(record); producer.close();
Example
This example shows how to send a simple text message to a Kafka topic named test-topic. It sets up the producer properties, creates the producer, sends the message, and closes the producer.
java
import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerRecord; import java.util.Properties; public class SimpleKafkaProducer { 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", "Hello Kafka!"); producer.send(record); System.out.println("Message sent to Kafka topic 'test-topic'"); producer.close(); } }
Output
Message sent to Kafka topic 'test-topic'
Common Pitfalls
- Not setting the correct
bootstrap.serversaddress causes connection failure. - Forgetting to specify serializers for key and value leads to serialization errors.
- Not closing the producer can cause resource leaks.
- Sending messages without specifying the topic will fail.
Always check Kafka broker availability and topic existence before sending messages.
java
/* Wrong: Missing serializers */ Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); KafkaProducer<String, String> producer = new KafkaProducer<>(props); // This will throw 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 sending messages to Kafka:
- Set
bootstrap.serversto your Kafka broker address. - Use proper serializers for your message key and value.
- Create a
ProducerRecordwith topic and message. - Call
send()to publish the message asynchronously. - Close the producer after sending messages.
Key Takeaways
Always configure KafkaProducer with broker address and serializers before sending messages.
Use ProducerRecord to specify the topic and message content.
Call send() method to publish messages asynchronously to Kafka.
Close the KafkaProducer to release resources after sending messages.
Check Kafka broker connectivity and topic existence to avoid errors.