【问题标题】:How to push data to azure event-hub for a node js application?如何将数据推送到节点 js 应用程序的天蓝色事件中心?
【发布时间】:2021-08-17 07:40:21
【问题描述】:

下面是 server.js 代码。下面的代码是否将数据推送到 Azure 事件中心?如何交叉检查推送的数据? 数据包含 data ={name:'name',dob:'01-12-2000}


    const { EventHubProducerClient } = 
    require("@azure/event-hubs");

    const connectionString = "EVENT HUBS 
    NAMESPACE CONNECTION STRING";
    const eventHubName = "EVENT HUB NAME";
    
    async function main() {

    // Create a producer client to send messages to the event hub.
  const producer = new EventHubProducerClient(connectionString, eventHubName);

  // Prepare a batch of three events.
  const batch = await producer.createBatch();
  batch.tryAdd({ body: "First event" });
  batch.tryAdd({ body: "Second event" });
  batch.tryAdd({ body: "Third event" });    

  // Send the batch to the event hub.
  await producer.sendBatch(batch);

  // Close the producer client.
  await producer.close();

  console.log("A batch of three events have been sent to the event hub");
}

main().catch((err) => {
  console.log("Error occurred: ", err);
});

【问题讨论】:

  • 请帮助.. 提前致谢

标签: node.js azure-eventhub


【解决方案1】:

是的,您共享的代码示例会将事件发布到事件中心。也就是说,忽略batch.tryAdd 的返回值是个坏主意,可能会导致数据丢失。您可能想查看publishing sample 作为推荐模式的说明。

通过“交叉检查”,我假设您希望回读事件并验证有效负载;您可以使用EventHubConsumerClient 来执行此操作,如receiving sample 中所示。

【讨论】:

  • 谢谢杰西!
猜你喜欢
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 2020-07-13
  • 2017-05-29
相关资源
最近更新 更多