如何在node js中将jsonArray写入kafka主题

Jam*_*ten 3 arrays json node.js apache-kafka

如何在节点js中将数组(具有json字符串)写入Kakfa主题?

小智 5

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)
  }
})
Run Code Online (Sandbox Code Playgroud)

消息值对于 Kafka 而言只是字节,因此我们需要将 JavaScript 对象序列化为 JSON 字符串。其他序列化方法(如 Avro)也可用。

只需将数组数据包装为JSON.stringify(array)并将其发送到值中,在消费者端您可以使用以下命令解析它JSON.parse(message.value)

以下是一些帮助链接https://www.confluence.io/blog/getting-started-with-kafkajs/

https://www.npmjs.com/package/kafka-node# Producer