Creating a Simple Kafka Producer in Java

Apache Kafka is a fault tolerant publish-subscribe streaming platform that lets you process streams of records as they occur. If you haven’t installed Kafka yet, see our Kafka Quickstart Tutorial to get up and running quickly.

In this post we discuss how to create a simple Kafka producer in Java.

Kafka Producer Java Code

The example below shows creating a Kafka producer object and using it to send messages to the my-topic topic.

In this example we are only using the required producer configuration parameters, which include the bootstrap.servers URI and the key and value Serializers. We are sending Integer keys and String values in each message which is why we use the IntegerSerializer and StringSerializer classes in our configuration parameters.

This example is placed inside a main() method to make it easy to run from the command line or from within an IDE.

Running the Kafka Producer Example

In order to run this example, we need a Zookeeper server and a Kafka server running. For an example of how to do this see our Kafka Quickstart Tutorial to get up and running.

Once we have Zookeeper and Kafka running we can create the my-topic topic:

We are now ready to execute our code and send messages to Kafka from the command line or from directly within an IDE. Below is an example of running this code on the command line from our bigdatums-kafka-1.0-SNAPSHOT.jar JAR file.

An easy way to see the messages sent by our producer is to use the kafka-console-consumer which will read messages from a Kafka topic and print them to stdout:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">