How to get key & value from Kafka RecordHeaders

Viewed 1982

I have a ProducerRecord object.

ProducerRecord<String, byte[]> hdr = addHeader.addMDGHeader(record);

I'm trying to write a test that checks a particular header key exists.

If I print hdr.headers().toString() I get the following RecordHeaders(headers = [RecordHeader(key = mdpHeader, value = [123, 34, 83, 101, 113, 117, 101, 110, 99, 101, 78, 111, 34, 58, 48, 44, 34, 84, 101, 109, 112, 108, 97, 116, 101, 115, 34, 58, 91, 93, 125])], isReadOnly = false).

How do I pull out mdpHeader?

1 Answers

The Header.value() method returns byte array byte[], and then you can convert it into string, you can see more examples here

String value = new String(header.value(), StandardCharsets.UTF_8);
Related