0

I send data towards an Azure IoTHub. This data is then retreived and processed by a IoTHub EventHub function.

This function retreives the data and insert this data into a Azure Cosmos DB.

In the IoTHub EventHub function you have to declare the Cosmos database and the Cosmos collection before the function can run.

The problem is that I want to use the a dynamic collection name. This name depends on the data which is send towards the IoTHub. Is this possible? Can I declare the collection name when the function is running?

With the scripts below it is possible to send towards one collection

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(); 

};

Summery:

I want to use a variable collectionName, that will declared in the index.js?

How do I declare the collectionName in de function.json and can I declare this variable in de index.js?

arjan kroon
  • 813
  • 2
  • 9
  • 22

2 Answers2

0

You could do that with C#/F# Azure Function by using Binder class (see this example), but the scenario is not supported for non-.NET languages yet.

There is a tracking item to enable this for Node.js Functions as well, but it's very old and there was no progress on it.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
0

When I log the context.bindData:

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

I get the following:

{
    "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"
}
arjan kroon
  • 813
  • 2
  • 9
  • 22