这是我的 Azure 管道:
IoTHub → EventHub src → JavaScript 中的 Azure Function → 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
中清楚地分离出这个值,以便进一步处理。
最佳答案
此 (context.bindings.eventHubDest = TransformedMessageBody
) 仅设置发送出去的消息的消息正文。这是一条全新的消息,而不仅仅是转发传入的消息。因此,所有元数据也会丢失。
阅读this听起来您在使用 Javascript 时无法绑定(bind)到 EventData
- 如 you can when using C#.绑定(bind)到 EventData 还允许您设置元数据,而绑定(bind)到正文字符串则不能。
所以我想如果您需要它,您需要使用 C#(Java 也可能有效)。或者使用完全不同的东西,例如 Azure 流分析。
关于azure-functions - 如何在 Azure Functions 中的 EventHub 输出绑定(bind)上设置 systemProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338607/