Setting Up a Kafka Producer and Sending Your First Message

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

  • Open Confluent Control Center (installed in a previous post).

  • Navigate to “Topics” from the home page.

  • Click “+ Add topic”, then enter your preferred topic name.

  • Click “Create with defaults.” For this simple guide, we won’t dive deeper into topic configuration.

Once created, you’ll see the new topic appear with zero messages.

2. Set Up a New Java Maven Project

  • Create a blank Maven project in your preferred IDE (e.g., IntelliJ IDEA).
  • In your pom.xml, add the following dependency:

<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

  • run mvn clean install to download all dependencies.
  • Run your Java application.
  • You should see an output similar to:

Offset: 0        

  • Open Confluent Control Center again and navigate back to your topic.
  • Select the “Messages” tab on the topic’s page.
  • Enter 0 in the “Jump to offsets” search bar and press Enter. You should see the message with the key and value you specified.

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

要查看或添加评论,请登录

Amr Ali Eissa的更多文章

社区洞察

其他会员也浏览了