Setting Up a Kafka Producer and Sending Your First Message
In this post, we’ll create a Kafka topic and send a message to it using a simple Java application. Follow the steps below to get started.
1. Create a Kafka Topic
Once created, you’ll see the new topic appear with zero messages.
2. Set Up a New Java Maven Project
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.8.0</version>
</dependency>
3. Define Kafka Producer Properties
Inside your Java application, after the main method, define a method to set up basic producer configurations:
领英推荐
private static Properties getProducerProperties() {
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
return props;
}
This method sets up the minimal configurations needed for the KafkaProducer.
4. Produce a Message to Your Topic
In the main method, add the following code to send your first message:
public static void main(String[] args) {
try (Producer<String, String> producer = new KafkaProducer<>(getProducerProperties())) {
String topic = "test-topic";
String key = "test-key";
String value = "test-value";
ProducerRecord<String, String> producerRecord = new ProducerRecord<>(topic, key, value);
RecordMetadata result = producer.send(producerRecord).get();
if (result.hasOffset()) {
System.out.println("Offset: " + result.offset());
} else {
System.out.println("Failed to produce a new message");
}
} catch (Exception e) {
System.out.println("Failed to produce a new testcase: " + e.getMessage());
}
}
5. Verify Your Message in Control Center
Offset: 0
Congratulations! You’ve successfully produced your first Kafka message using Java.
That’s it for this post! You now have a basic Java-based producer that can publish messages to a Kafka topic. Stay tuned for more Kafka tutorials!
GitHub repo for above -> KafkaWizAmr/SimpleKafkaProducer
For more exclusives articles visit -> Home - KafkaWizAmr