[英]How to write a jsonArray to kafka topic in node js
如何在節點 js 中將數組(具有 json 字符串)寫入 Kakfa 主題?
server.on('package:publish', async event => {
try {
const responses = await producer.send({
topic: process.env.TOPIC,
messages: [{
// Name of the published package as key, to make sure that we process events in order
key: event.name,
// The message value is just bytes to Kafka, so we need to serialize our JavaScript
// object to a JSON string. Other serialization methods like Avro are available.
value: JSON.stringify(array)
}]
})
console.log('Published message', { responses })
} catch (error) {
console.error('Error publishing message', error)
}
})
消息值只是 Kafka 的字節,因此我們需要將 JavaScript object 序列化為 JSON 字符串。 其他序列化方法(如 Avro)也可用。
只需將您的數組數據包裝為JSON.stringify(array)
並將其發送為值,然后在消費者端您可以使用JSON.parse(message.value)
對其進行解析
這里有一些幫助鏈接https://www.confluent.io/blog/getting-started-with-kafkajs/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.