Kafka01 - Kafka producer

Getting producer - Kafka's

Preface:

  Kafka was born so far to produce two versions of the producer client: 1 is based on early customer scala language end; 2, with the influx of a wide range of Java users, kafka0.9 version began to withdraw from Java version of the client;

  A substantially producer producer logic requires the following basic conditions:

    • Configure Producer, producers create instances;
    • Construction of message to be sent;
    • Send a message;
    • Examples Close producers;

 

KafkaProducer necessary parameters:

  • bootstrap.servers: broker address to be connected;

  • key.serializer and value.serializer: kafka messages need to be converted to byte array byte [] is passed, typically used to change the parameter value of the specified key and serialization. Parameters corresponding to the configuration of the server need to deserialize the parameters, they are corresponding.

  KafkaProducer is thread safe, multiple threads may share a single instance KafkaProducer, or by pooling, so convenient to use. , Can be performed in actual use by Java configuration at the project start the initialization of the production side, to generate corresponding instance.

KafkaProducer send a message:

  Kafka message transmission needs to achieve the object by constructing ProducerRecoder, properties of the class are:    

    private final String topic;
    private final Integer partition;
    private final Headers headers;
    private final K key;
    private final V value;
    private final Long timestamp;

 

  Among them, the most simple method of using only need to specify the topic and message body can be. `ProducerRecord <String, String> record = new ProducerRecord <> (topic," hello, Kafka! ");

`

 

  

Guess you like

Origin www.cnblogs.com/whtblog/p/11409356.html