Producer - Tuong-Nguyen/Angular-D3-Cometd GitHub Wiki

Producer

Producer Internals


// Connection information
Properties props = new Properties();
props.put(“bootstrap.servers”, “BROKER-1:9092, BROKER-2:9093”);
props.put(“key.serializer”, “org.apache.kafka.common.serialization.StringSerializer”);
props.put(“value.serializer”, “org.apache.kafka.common.serialization.StringSerializer”);

// Create an Producer
public class KafkaProducerApp {
  public static void main(String[] args){
    Properties props = new Properties();
    props.put(“bootstrap.servers”, “BROKER-1:9092, BROKER-2:9093”);
    props.put(“key.serializer”, “org.apache.kafka.common.serialization.StringSerializer”);
    props.put(“value.serializer”, “org.apache.kafka.common.serialization.StringSerializer”);

    KafkaProducer myProducer = new KafkaProducer(props);
  }
}

Partitioning Strategy

This image shows how messages are distributed into partitions:

Partitioning Strategy

By default (if not specify), Round Robin is applied.
If producer specifies the partition to send the message, that partition is used.
If message key is specified, partitioner will parse the key to specify which partition to send the message. (The partitioner can be customed or a default is used)

Message buffering

Messages are batched before sending to server for efficiently transport.

Message buffering

The batch is sent when:

  • Batch size is reached.
  • Buffer size is reached.
  • Timeout is reached.