Azure IoTHub函数Cosmos DB(js)

时间:2018-05-23 08:44:56

标签: azure azure-functions azure-cosmosdb azure-iot-hub azure-functions-runtime

我向Azure IoTHub发送数据。 然后通过IoTHub EventHub函数对该数据进行检索和处理。

此功能可以检索数据并将此数据插入Azure Cosmos DB。

在IoTHub EventHub函数中,您必须在函数运行之前声明Cosmos数据库和Cosmos集合。

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

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

function.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中的变量?

2 个答案:

答案 0 :(得分:0)

您可以使用Binder类(请参阅this example)使用C#/ F#Azure函数执行此操作,但非.NET语言尚不支持该方案。

还有tracking item为Node.js功能启用此功能,但它已经很老了,并且没有进展。

答案 1 :(得分: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"
}