I'm new to Kafka. I'm a little confused about the kafka message format. I tested a KafkaJS consumer.
const run = async () => {
await kafkaClient.consumer.subscribe({ topic: 'mytopic', fromBeginning: true })
await kafkaClient.consumer.run({
eachBatchAutoResolve: false,
eachBatch: async ({ batch, resolveOffset, heartbeat, isRunning, isStale }) => {
for (let message of batch.messages) {
if (!isRunning() || isStale()) break
processMessage(message)
resolveOffset(message.offset)
}
},
})
}
I use console.log(message)
to see the message format and it's like this
{
magicByte: 2,
attributes: 0,
timestamp: '540669',
offset: '601953',
key: <Buffer 39 63 37 23>,
value: <Buffer 7b 65 32 65 37 38 ... 555 more bytes>,
headers: {
'myheader': <Buffer 61 6f>,
},
‧‧‧‧‧‧
}
I also tried a consumer built with spring boot on localhost. As there is no producer, I used Postman to send messages to kafka. The message received by spring boot consumer is like this
{
body: 'this is body',
clientIp: 'this is IP
}
'this is body'
is the content I sent from Postman. The value of clientIp
is my ip.
I notice this is the content returned from message.value.toString()
in KafkaJS.
Why are they different? Will consumers built with different framework get different message if they connect to the same kafka topic?
What should I try if I want to build a java consumer to receive and consume the same message format as a KafkaJS consumer?