header-logo.png GKINDEX

Apache kafka Producer

In the publish and subscribe messaging system, the flow starts from the Publisher(which sends a piece of message to the broker(Kafka)).

Kafka comes with Client API , which can interm help to communicate with Kafka. The following is the example for the Kafka Producer, which will help you to understand how we are communicating with Kafka while sending the message(Record).

Kafka Producer example

In the above example, we are creating four properties to the Properties Class. The following is the description and use cases of those four properties.

bootstrap.servers

bootstrap.servers helps to provide the Kafka broker details list, we can pass the list of brokers as well with "," separated.

Ex:- "localhost:9092,localhost:9093,localhost:9094,localhost:9092"

We can pass as many brokers as we want based on the load. For now just dont go indepth about how the multiple brokers works later chapters we can see those.

client.id

client.id Can be anything which will be helpful to identify from which Client we sent the message to the Kafka.

key.serializer

By default kafka expects both key and value as a byte array but we can pass the custom class as well.But the custom class should have some specific behavior(Later chapters we can learn). key.serializer value should be the name of the class which is used for serializing the Key. For the example we have used default implementation from the Kafka Client API.

value.serializer

value.serializer value should be the name of the class which is used for serializing the Key, or the example we have used default implementation from the Kafka Client API.

The above example will not gurantee you that whether message reached to kafka or not without any error message.
  • Share