Kafka

Producer API

Producer API

Producer class forms the central part of the Producer API in Kafka and helps in forming connections with Kafka brokers. 

 

Using the ‘send method’, the Producer class can send messages to one or more topics. Here’s the syntax -

 

public void send(KeyedMessaget<k,v> message) 

- sends the data to a single topic,par-titioned by key using either sync or async producer.

public void send(List<KeyedMessage<k,v>>messages)

- sends data to multiple topics.

Properties prop = new Properties();

prop.put(producer.type,”async”)

ProducerConfig config = new ProducerConfig(prop);

 

Using the ‘close method’ ensures that all the connections with the Kafka brokers are closed. Here’s the syntax for the close method -

 

public void close()