How to produce messages with headers in Kafka 0.11 using console producer?
I didn't find any description in Kafka document about this.
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();
}
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 -f testkafkaproducerfile.json
for more information check github page: https://github.com/edenhill/kcat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With