Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

apache-kafka

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

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

like image 412
cbb Avatar asked Dec 19 '22 00:12

cbb


2 Answers

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();
}
like image 55
Mickael Maison Avatar answered May 30 '23 10:05

Mickael Maison


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

like image 43
k2rubyj Avatar answered May 30 '23 09:05

k2rubyj