Adding Custom Headers in Kafka Message

Kafka v0.11.0.0 adds support for custom headers.

You can add them when creating a ProducerRecord like this:

new ProducerRecord(key, value, headers, ...), where headers is of type Iterable<Header>

For more details see:

https://issues.apache.org/jira/browse/KAFKA-4208

https://cwiki.apache.org/confluence/display/KAFKA/KIP-82+-+Add+Record+Headers


Record level headers were introduced from Kafka 0.11.0. We can send a list of Headers in each record.

List<Header> headers = Arrays.asList(new RecordHeader("header_key", "header_value".getBytes()));
ProducerRecord<String, String> record = new ProducerRecord<>("topic", null, "key", "value", headers);

Tags:

Apache Kafka