runnerpaul
runnerpaul

Reputation: 7236

How to get key & value from Kafka RecordHeaders

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?

Upvotes: 5

Views: 7974

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40078

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);

Upvotes: 7

Related Questions