cbb
cbb

Reputation: 141

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

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

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

Upvotes: 5

Views: 16108

Answers (2)

k2rubyj
k2rubyj

Reputation: 71

You can also use kcat to produce message with header.

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

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

Upvotes: 7

Mickael Maison
Mickael Maison

Reputation: 26885

Update: Since Kafka 3.2, you can produce records with headers using the kafka-console-producer.sh tool. For details, see https://cwiki.apache.org/confluence/display/KAFKA/KIP-798%3A+Add+possibility+to+write+kafka+headers+in+Kafka+Console+Producer


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();
}

Upvotes: 9

Related Questions