14

I'd like to use the kafka-console-producer.sh to fire a few JSON messages with Kafka headers.

Is this possible?

docker exec -it kafka_1 /opt/kafka_2.12-2.3.0/bin/kafka-console-producer.sh --broker-list localhost:9093 --topic my-topic --producer.config /opt/kafka_2.12-2.3.0/config/my-custom.properties
Jagadesh
  • 2,104
  • 1
  • 16
  • 28
tosi
  • 1,873
  • 2
  • 18
  • 38
  • 3
    Possible duplicate of [How to produce messages with headers in Kafka 0.11 using console producer?](https://stackoverflow.com/questions/49217828/how-to-produce-messages-with-headers-in-kafka-0-11-using-console-producer) – Mickael Maison Nov 05 '19 at 17:58

3 Answers3

12

No, but you can with kafkacat's -H argument:

Produce:

echo '{"col_foo":1}'|kafkacat -b localhost:9092 -t test -P -H foo=bar

Consume:

kafkacat -b localhost:9092 -t test -C -f '-----\nTopic %t[%p]\nOffset: %o\nHeaders: %h\nKey: %k\nPayload (%S bytes): %s\n'
-----
Topic test[0]
Offset: 0
Headers: foo=bar
Key:
Payload (9 bytes): col_foo:1
% Reached end of topic test [0] at offset 1
so-random-dude
  • 15,277
  • 10
  • 68
  • 113
Robin Moffatt
  • 30,382
  • 3
  • 65
  • 92
1

Starting from kafka 3.1.0 there is an option to turn on headers parsing parse.headers=true and then you just place them before your record value, info form docs:

| parse.headers=true:
|  "h1:v1,h2:v2...\tvalue"

So your command will look like

kafka-console-producer.sh --bootstrap-server localhost:9092 --topic topic_name --property parse.headers=true

and then you pass

header_name:header_value\nrecord_value

dswiecki
  • 138
  • 2
  • 11
  • 1
    for what it's worth. The default property for `headers.delimiter` is `\t`. After some confusion. This command will need --property headers.delimiter='\n' passed along as well otherwise the CLI complains with exceptions regarding the header delimiter. – madeyejm Aug 08 '22 at 19:03
  • I am getting No headers delimiter found on line number 1: with this message "key:value\t sa" – saurav Sep 13 '22 at 12:16
  • https://cwiki.apache.org/confluence/display/KAFKA/KIP-798%3A+Add+possibility+to+write+kafka+headers+in+Kafka+Console+Producer – Srinivasu Oct 01 '22 at 06:53
  • Can you add one example with header as json payload and value as json payload as well. – dinesh kandpal Nov 25 '22 at 15:43
0

Consumer producer command:

docker exec -it container_id kafka-console-producer.sh --topic my-topic --bootstrap-server localhost:9092 --property parse.headers=true --property headers.key.separator=: --property headers.delimiter=\t

Payload format:

header_name:header_value t payload_value

Console consumer command:

docker exec -it container_id kafka-console-consumer.sh --topic my-topic --bootstrap-server localhost:9092 --property print.headers=true
Sambhav Khare
  • 61
  • 3
  • 9