0

我向 Azure IoTHub 发送数据。然后由 IoTHub EventHub 函数检索和处理此数据。

此函数检索数据并将此数据插入 Azure Cosmos DB。

在 IoTHub EventHub 函数中,您必须先声明 Cosmos 数据库和 Cosmos 集合,然后才能运行该函数。

问题是我想使用动态集合名称。此名称取决于发送到 IoTHub 的数据。这可能吗?我可以在函数运行时声明集合名称吗?

使用下面的脚本可以发送到一个集合

函数.json:

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "IoTHubMessages",
      "direction": "in",
      "path": "poc_funceventhubname",
      "connection": "POCIoTHub_events_IOTHUB",
      "cardinality": "many",
      "consumerGroup": "functions"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "VALUES",
      "collectionName": "POCVALUES",
      "createIfNotExists": true,
      "connection": "pocCosmos_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

index.js:

module.exports = function (context, IoTHubMessages) {
    var v;
    var output = [];
    IoTHubMessages.forEach(message => {
        v = message.v;
        context.log(`v = ${v}`);
        for(var i = 0; i < message.REGS.length; i++) {
            var obj = message.REGS[i];
            output[i] = {
                    "vi": v,
                    "pi": obj[0],
                    "ts": obj[2],
                    "vl": obj[1]
                };
            context.bindings.outputDocument = output;
        }
    });

    context.done(); 

};

总结:

我想使用一个变量 collectionName,它将在 index.js 中声明?

如何在 de function.json 中声明 collectionName,我可以在 de index.js 中声明这个变量吗?

4

2 回答 2

0

当我记录 context.bindData 时:

(context.log(`Bindingdata = ${JSON.stringify(context.bindingData)}`))

我得到以下信息:

{
    "partitionContext": {
        "eventHubPath": "tdppoc1iothub",
        "consumerGroupName": "functions"
    },
    "partitionKeyArray": [
        null,
        null
    ],
    "offsetArray": [
        "17206545352",
        "17206546560"
    ],
    "sequenceNumberArray": [
        296710,
        296711
    ],
    "enqueuedTimeUtcArray": [
        "2018-05-23T11:28:59.271Z",
        "2018-05-23T11:29:01.317Z"
    ],
    "propertiesArray": [
        {},
        {}
    ],
    "systemPropertiesArray": [
        {
            "iothub-connection-device-id": "tdppoc1device1",
            "iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
            "iothub-connection-auth-generation-id": "********",
            "iothub-enqueuedtime": "2018-05-23T11:28:59.015Z",
            "iothub-message-source": "Telemetry",
            "x-opt-sequence-number": 296710,
            "x-opt-offset": "17206545352",
            "x-opt-enqueued-time": "2018-05-23T11:28:59.271Z",
            "enqueuedTimeUtc": "2018-05-23T11:28:59.271Z",
            "sequenceNumber": 296710,
            "offset": "17206545352"
        },
        {
            "iothub-connection-device-id": "tdppoc1device1",
            "iothub-connection-auth-method": "{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
            "iothub-connection-auth-generation-id": "********",
            "iothub-enqueuedtime": "2018-05-23T11:29:01.015Z",
            "iothub-message-source": "Telemetry",
            "x-opt-sequence-number": 296711,
            "x-opt-offset": "17206546560",
            "x-opt-enqueued-time": "2018-05-23T11:29:01.317Z",
            "enqueuedTimeUtc": "2018-05-23T11:29:01.317Z",
            "sequenceNumber": 296711,
            "offset": "17206546560"
        }
    ],
    "sys": {
        "methodName": "IoTHubJS_EventHubPOC",
        "utcNow": "2018-05-23T11:29:01.610Z"
    },
    "invocationId": "ea783bf0-3354-4bb9-bba5-e4273760c14a"
}
于 2018-05-23T11:32:55.223 回答
0

您可以通过使用Binder类使用 C#/F# Azure Function 来执行此操作(请参阅此示例),但非 .NET 语言尚不支持该方案。

也有一个跟踪项可以为 Node.js Functions 启用此功能,但它非常旧,并且没有任何进展。

于 2018-05-23T09:04:04.913 回答