Introduction to the ProducerRecord object that constructs messages

public class ProducerRecord<K.V> {

    private final String topic;
    private final Integer partition;
    private final Headers headers;
    private final K key;
    private final V value;
    private final Long timestamp;
	xxx
}
Copy the code

The topic and partition fields represent the topic and partition number to which the message is sent, respectively.

The HEADERS field is the header of a message and is used to set some application-specific information, if not necessary

Key is used to specify the key of the message. It is not only additional information about the message, but also can be used to calculate the partition number so that the message can be sent to a specific partition. As mentioned above, messages are categorized by topic, but this key allows messages to be categorized twice. Messages with the same key are grouped into the same partition. Messages with keys can also support log compression.

Value is the body of the message and is generally not empty. If it is empty, it indicates a specific message-tombstone message.

Timestamp is a timestamp only and has two types: CreateTime and LogAppendTime. The former represents when the message was created and the latter represents when the message was appended to the log file.

Topic and Value are mandatory.

KafkaProducer is thread-safe. A single KafkaProducer can be shared between multiple threads, or KafkaProducer instances can be pooled for invocation by other threads.