In this article, we will learn how to write Kafka producer in Java language. We will create test topic then we will build our producer and set its properties. After that, we will start sending messages to that producer. We will also see how we can use Keys to send messages to the same partition in particular topic.

Creating topic

In Kafka, Producers send records to the topic. So we need one topic to which our producer sends records. You can use this code to create a new topic in Kafka server. In next steps, it is assumed that you have created the topic with name myTopic and we are going to use that to send messages from the producer.

Kafka Producer

Now we can start creating our own Kafka producer in Java. Let us see, how to set up Kafka Producer and set its properties.

In above code, we can see that we have set up properties for the producer. We need to pass the list of the bootstrap server so that producer can connect to them and start sending messages. In this example, we are using producer which uses a key as well as messages in String format so that we are using String Serializer. CLIENT_ID_CONFIG property, we are setting simple name to our producer in Kafka server. In case of failure to send a message to Kafka topic, we want to try sending that message again. By setting RETRIES_CONFIG property, we can guarantee that in case of failure this producer will try sending that message two more times. You can learn more about Kafka Producers and their configuration here.

Sending messages

We need to create ProducerRecord to send messages to Kafka topic.

Here we have created producer record and we are sending our first message to Kafka topic. You can notice that we have not passed any key in this producer record. In the absence of the key, this message will be sent to any partitions in that topic. If we want to send the same type of messages to same partitions in that topic then we need to pass key with that message.

All messages with key = 1 will be assigned to the same partition of this topic. Send function of producer returns recordmetadata which has information to which partition our message went. We can read that using following code.

Conclusion

In this tutorial, we have created one simple Kafka producer in Java and understand its different configurations. you can download the code used in this article at git repository. In next article, We will learn how to write consumer using Java and read messages from Kafka topics. Till then, Happy learning!!

I am passionate about data analytics, machine learning, and artificial intelligence. Recently I have started blogging about my experience while learning these exciting technologies.

Leave a Reply

Close Menu