1.

Explain producer API in Kafka?

Answer»

The core part of Kafka producer API is “KafkaProducer” CLASS. Once we instantiate this class, it allows the option to connect to Kafka broker inside its constructor. It has the method “send” which allows the producer system to send messages to topic asynchronously: 

  • ProducerRecord- This class represent streams of records to be sent
  • Callback- This function is called when the server acknowledges the message.

The Kafka producer has one flush method which is used to ensure previously sent messages are cleared from the buffer.

  • The Producer API- The core class of this API is the “Producer” class. This class also has a send method to send messages to single or multiple topics :
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);

The producer is broadly classified into two types: Sync & Async

A message is sent directly to the broker in sync producer while it passes through in the background in case of an async producer. Async producer is used in case we need high throughput

The following are the configuration settings listed in producer API :

S.NoConfiguration Settings and Description
1

client.id

identifies producer application

2

producer.type

either sync or async

3

acks

The acks config controls the criteria under producer requests are con-sidered complete.

4

retries

If producer request fails, then automatically RETRY with specific value.

5

bootstrap.servers

bootstrapping list of brokers.

6

linger.ms

if you want to reduce the number of requests you can set linger.ms to something GREATER than some value.

7

key.serializer

Key for the serializer interface.

8

value.serializer

value for the serializer interface.

9

batch.size

Buffer size.

10

buffer.memory

controls the total amount of memory available to the producer for buff-ering.

  • The Produce Record API: This  API is used for sending key-value pair to cluster. This class has three different constructors:
public ProducerRecord (string topic, int partition, k key, v value)
  • Topic − user defined topic name that will appended to record.
  • Partition − partition count
  • Key − The key that will be included in the record.
  • Value − Record contents
public ProducerRecord (string topic, k key, v value)

ProducerRecord class constructor is used to create a record with key, value pairs and without partition.

  • Topic − Create a topic to assign record.
  • Key − key for the record.
  • Value − record contents.
public ProducerRecord (string topic, v value)

ProducerRecord class creates a record without partition and key.

  • Topic − create a topic.
  • Value − record contents.


Discussion

No Comment Found