The following blog will make you understand about one of the three approaches used to send data on Kafka i.e. Send and Forget. You will also see the how this approach is implemented through java programming Language.
Send and Forget
Send and forget approach is the most simple approach. In this approach, we send a message to kafka broker and don’t care whether the message is received by the kafka broker or not. If you are not bothering about the success or failure of messages received then you may ask why someone will use the approach and more importantly where to use this approach. You know that kafka is distributed system and due to its own fault tolerance it is highly available. So the probability of losing messages is less and you can use this approach where losing some messages is not a big issue for you. There are many use cases where it can be used like you are using twitter data for sentiment analysis. Then there if you are loosing 1-2% of tweets then it will not affect your sentiment analysis. Please keep in mind that if you can’t afford to lose some messages then don’t go for send and forget approach.
Implementation using Java
1. Import Necessary java Packages;
2. Create variables which will define the topicName, key and value; It is already known to you that kafka accepts messages in the form of key and value pair. The key and value pair is nothing but it is your message which you want to send to kafka.
3. Create Properties class object and put some configurations into it.
a) bootstrap.servers is the list of kafka brokers addresses.
b) StringSerializer is the class which is used to serialize your key and value. It is known to you that kafka accepts the data in the form of array of bytes. So the process of converting your message into bytes is known as serialization.
4) Create KafkaProducer object and constructor of KafkaProducer takes the properties class object as an argument.
5) Create ProducerRecord Object and constructor of ProducerRecord takes three values as an argument. Your topicName, key and your value.
6) Now use send method of Producer and pass the ProducerRecord object as an argument.
7) Lastly, run your java program and in order to test your program just run the kafka console consumer on your terminal and you will see that the message you have written in your program will be on that kafka console consumer. If you have any confusion then pls refer the whole java program as shown below.
Hope Now you get an idea to send data to kafka through Send and Forget approach. There are 2 more approaches which is used to send data to kafka. I will be covering all of them one by one along with there differences in upcoming parts of blog. Till then keep reading Kafka.