How to produce messages with headers in Kafka 0.11

2020-07-24 06:07发布

问题:

How to produce messages with headers in Kafka 0.11 using console producer?

I didn't find any description in Kafka document about this.

回答1:

Using the kafka-console-producer.sh tool (ConsoleProducer.scala) you cannot produce messages with headers.

You need to write your own small application. Headers are passed in when creating a ProducerRecord. For example:

public static void main(String[] args) throws Exception {
    Properties producerConfig = new Properties();
    producerConfig.load(new FileInputStream("producer.properties"));

    KafkaProducer<String, String> producer = new KafkaProducer<>(producerConfig);

    List<Header> headers = Arrays.asList(new RecordHeader("header_key", "header_value".getBytes()));
    ProducerRecord<String, String> record = new ProducerRecord<>("topic", 0, "key", "value", headers);
    Future<RecordMetadata> future = producer.send(record);
    future.get();

    producer.close();
}


回答2:

You can also use kafkacat to produce message with header.

kafkacat -P -b localhost:9092 -H "facilityCountryCode=US" -H "facilityNum=32619" -t test.topic.to.mq -f testkafkaproducerfile.json

for more information check github page: https://github.com/edenhill/kafkacat