【问题标题】:Azure IoTHub Functions Cosmos DB (js)Azure IoTHub 函数 Cosmos DB (js)
【发布时间】:2018-05-23 08:44:56
【问题描述】:

我向 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 中的变量?

【问题讨论】:

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


    【解决方案1】:

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

    有一个tracking item 也可以为 Node.js 函数启用此功能,但它很旧,没有任何进展。

    【讨论】:

      【解决方案2】:

      当我记录 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"
      }
      

      【讨论】:

        猜你喜欢
        • 2021-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-20
        • 1970-01-01
        • 1970-01-01
        • 2019-01-04
        • 2019-01-04
        相关资源
        最近更新 更多