【问题标题】:Azure Event Hub 5.6.0 - How to send an event to a specific partition id Nodejs?Azure 事件中心 5.6.0 - 如何将事件发送到特定分区 ID Nodejs?
【发布时间】:2022-01-11 18:55:24
【问题描述】:

我在 nodejs 中使用 azure/event-hubs 1.0.8 版本将数据发送到特定的分区 ID。下面是示例代码

请有人帮助我如何使用 nodejs 中的 azure/event-hubs 5.6.0 版本将数据发送到特定分区 ID?我研究了很多,但找不到任何来源。任何帮助将不胜感激!

以下是 EventHub 5.6.0 的代码

 const insertOneInPartition = (data, partition, eventsHub) => {
  const dataToInsert = { body: { ...data } };
  return new Promise((resolve, reject) => {
    eventsHub.createBatch({ partitionId : partition }).then(async eventDataBatch => {
      if (data) {
        eventDataBatch.tryAdd(dataToInsert);
        await eventsHub.sendBatch(eventDataBatch);
        resolve(eventsHub);
      } else {
        // eslint-disable-next-line prefer-promise-reject-errors
        reject('error');
      }
    }).catch(error => {
      reject(error);
    });
  });
};

【问题讨论】:

    标签: javascript node.js azure azure-eventhub


    【解决方案1】:

    对于nodejs 客户端,请参阅createBatch(),它采用可以包含 partitionId 或分区键的选项参数,请参阅github 处的 sendEvents 示例代码,请参阅第 41 行。您可以使用此浏览器应用程序将测试消息发送到您的特定分区。

    对于Javascript 客户端,您应该在创建批处理时传递partition ID。将带有分区 id 的 batchOptions 框起来并将其发送到create_batch(batchOptions),请参阅第 35 行以在 https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/eventhub/event-hubs/samples/v5/javascript/sendEvents.js 的代码示例中设置 partitionId

    【讨论】:

    • 我在描述中添加了 EventHub 5.6.0 的示例代码。请你接受并编辑它
    • 查看第41行,了解发送消息时如何传递partitionId,github.com/Azure/azure-sdk-for-js/blob/…
    • 了解我已经在问题描述中编辑了我的示例代码。对吗?
    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    相关资源
    最近更新 更多