Producers are applications that create messages and publish them to the broker. Normally, producers are: frontend applications, web pages, web services, backend services, proxies, and adapters to legacy systems. We can write Kafka producers in Java, Scala, C and Python.
The process begins when the producer connects to any live node and requests the metadata about the partitions leaders of a topic, to put the message directly to the partition lead broker.
First, we need to understand the classes needed to write a producer:
- Producer: The producer class is
KafkaProducer <K, V>
inorg.apache.kafka.clients.producer.KafkaProducer
Kafka Producer is a type of Java Generic written in Scala, K specifies the type of the partition key and V specifies the message value.
ProducerRecord
: The class isProducerRecord <K, V>
inorg.apache.kafka.clients.producer.ProducerRecord
This class encapsulates the data required for establishing the connection with the brokers (broker list...