【问题标题】:Azure event hub Azure function node js integration not workingAzure 事件中心 Azure 功能节点 js 集成不起作用
【发布时间】:2020-07-02 02:24:22
【问题描述】:

我正在尝试使用计时器触发的 azure 函数从 azure eventthub 获取事件。在本地机器上运行时,我可以使用简单的 nodejs 代码成功获取事件。但是,如果我通过 node js azure 函数执行相同的代码,它就不起作用。我收到以下错误消息。有什么我想念的吗?

TypeError: EventHubConsumerClient is not a constructor' Stack: TypeError: EventHubConsumerClient is not a constructor at Object.<anonymous>

下面是示例代码

const { ContainerClient } = require("@azure/storage-blob");    
const { BlobCheckpointStore } = require("@azure/eventhubs-checkpointstore-blob");

const connectionString = "Endpoint=xxxx";    
const eventHubName = "yyyy";
const consumerGroup = "default";
const storageConnectionString = "abcd";
const containerName = "eventhubcontainer";

module.exports = async function (context, myTimer) {

const containerClient = new ContainerClient(storageConnectionString, containerName);

const checkpointStore = new BlobCheckpointStore(containerClient);
const consumerClient = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName, checkpointStore);
}

请帮忙

【问题讨论】:

    标签: javascript node.js azure azure-functions azure-eventhub


    【解决方案1】:

    你错过了这一行。

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

    【讨论】:

      【解决方案2】:

      您只需将 consumerGroup 和 ConnectionString 传递给 EventHubConsumerClient 构造函数

      const consumerClient = new EventHubConsumerClient(this.consumerGroup, eventHubConnectionString);
      

      这是Example

      【讨论】:

      • 遇到了问题,EventHubConsumerClient 包现在已在 azure 函数中自动下载。需要添加一个 package.json 并手动运行 npm install 来下载它。
      • 另一个问题 - 是否可以将消息发送到事件中心到特定分区?有人可以提供示例代码吗?
      • 您可以在传递给createBatch() 或sendBatch() 方法的选项中设置partitionId EventHubProducerClient。如果您想安全地批处理消息以使其符合最大大小限制,请使用 createBatch()。否则,通过传递消息数组直接使用 sendBatch()。见docs.microsoft.com/en-us/javascript/api/@azure/event-hubs/… 和docs.microsoft.com/en-us/javascript/api/@azure/event-hubs/…
      猜你喜欢
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2023-03-25
      • 2022-01-03
      相关资源
      最近更新 更多