Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 1.22 KB

File metadata and controls

28 lines (22 loc) · 1.22 KB

Producer API

  • Using Producer API, push the message to a topic (1 or more) in the Kafka.
    Properties props = new Properties();
    props.put("bootstrap.servers","localhost:9092");
    props.put("acks","all");
    props.put("retries",0);
    props.put("batch.size",16384);
    props.put("linger.ms",1);
    props.put("buffer.memory",33554432);
    props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
    
    Producer<String, String> producer = new KafkaProducer<>(props);
    
    for(int i = 0; i < 100; i++) {
        producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), Integer.toString(i)));
    }

    producer.close();

Metadata Request

  • The producer sends a Metadata request with a list of topics to one of the brokers in the broker-list you supplied when configuring the producer.
  • The broker responds with a list of partitions in those topics and the leader for each partition.
  • The producer caches this information and knows where to redirect its produce messages.