【问题标题】:How to set systemProperties on an EventHub out bindings in an Azure Function如何在 Azure 函数中的 EventHub 输出绑定上设置 systemProperties
【发布时间】:2020-02-22 02:28:17
【问题描述】:

这是我的 Azure 管道:

IoTHub → EventHub src → JavaScript 中的 Azure 函数 → EventHub dest

在 Azure 函数中,我需要对作为输入接收的事件主体应用一些转换,并将转换后的事件放入目标 EventHub。

通过context.bindingData.systemPropertiesArray,我可以检索iothub-connection-device-id 等基本元数据:

module.exports = async function (context, messageBodies) {
   messageBodies.forEach((messageBody, index) => {
        const transformedMessageBody = transform(messageBody)
        const deviceId = context.bindingData.systemPropertiesArray[index]['iothub-connection-device-id']
        context.log(deviceId)

        // here I can only set the messageBody. What's the way to attach back the original systemProperties?
        context.bindings.eventHubDest = transformedMessageBody
    })
}

问题systemProperties 在目标 EventHub 中丢失,因为我可以找到一种方法将它们重新设置在 Azure 函数代码中:

来源 EventHub 事件:

   {
      body: { foo: 'bar' },
      properties: undefined,
      offset: '143360',
      sequenceNumber: 392,
      enqueuedTimeUtc: 2020-02-21T11:30:32.294Z,
      partitionKey: undefined,
      systemProperties: {
        'iothub-connection-device-id': 'qux',
        'iothub-connection-auth-method': '{"scope":"device","type":"sas","issuer":"iothub","acceptingIpFilterRule":null}',
        'iothub-connection-auth-generation-id': '637177867069071846',
        'iothub-enqueuedtime': 1582284632134,
        'iothub-message-source': 'Telemetry'
      }
    }

目标 EventHub 事件:

   {
      body: { foo: 'transformedBar' },
      properties: undefined,
      offset: '34',
      sequenceNumber: 456,
      enqueuedTimeUtc: 2020-02-21T11:30:33.256Z,
      partitionKey: undefined,
      systemProperties: undefined
   }

注意:我知道我可以“作弊”并将 deviceId 附加到新事件的正文中, 但我需要在systemProperties 中将这个值完全分开,以便进一步处理。

【问题讨论】:

    标签: azure-functions azure-iot-hub azure-eventhub


    【解决方案1】:

    这 (context.bindings.eventHubDest = transformedMessageBody) 仅设置发送出去的消息的消息正文。这是一条全新的消息,而不仅仅是转发传入的消息。因此,任何元数据也会丢失。

    阅读 this 听起来您在使用 Javascript 时无法绑定到 EventData - 因为 you can when using C#. 绑定到 EventData 使您还可以设置元数据,而绑定到正文字符串则不能。

    所以我想如果你需要,你需要使用 C#(Java 也可以工作)。或者使用完全不同的东西,例如 Azure 流分析。

    【讨论】:

    • 我尝试了一个 C# Azure 函数。 SystemProperties 即使不接触输入事件也会丢失。因此,正如 Serkant Karaca 所说,必须将输入事件中的 SystemProperties 复制到输出绑定 Properties
    • 这就是我写“这是一条全新的消息,而不仅仅是转发传入的消息”的原因。但是 Serkant 是正确的,您只能设置“应用程序/用户定义的属性”,而不是系统属性
    【解决方案2】:

    无法在事件中心发布者上设置 SystemProperties,因为它仅供服务使用。我不确定它是如何在 JS 触发器中完成的,但是您应该改用 eventData.properties 包。

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 2018-05-01
      • 2017-07-24
      • 2021-02-09
      相关资源
      最近更新 更多