Search code examples
apache-kafkakafka-producer-api

Is there a way to add headers in kafka-console-producer.sh


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

Solution

  • 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