拆分字符串Azure函数以将数据插入Azure表存储

时间:2018-01-30 09:03:10

标签: javascript azure azure-table-storage

我尝试使用Azure功能从IOT Hub插入数据,以便使用此功能将数据插入Azure表存储

    'use strict';

    // This function is triggered each time a message is received in the IoT hub.
    // The message payload is persisted in an Azure storage table

    module.exports = function (context, iotHubMessage) {
    context.log('Message received: ' + JSON.stringify(iotHubMessage));
    var date = Date.now();
    var partitionKey = Math.floor(date / (24 * 60 * 60 * 1000)) + '';
    var rowKey = date + '';
    context.bindings.outputTable = {
     "partitionKey": partitionKey,
     "rowKey": rowKey,
     "message":  JSON.stringify(iotHubMessage)
    };
    context.done();
    };

确实有效,但数据只在一个字符串中

    [{"gwName":"Gateway-001","wellName":"AI-07","wellId":"BTG-80","pressure":39.02,"flow":0,"temperature":146.55,"rtc":1,"sdcard":1,"timestamp":1516562384}]

我想通过它的列分隔每个数据,所以我试试这个

    module.exports = function (context, iotHubMessage) {
       context.log('Message received: ' + JSON.stringify(iotHubMessage));
       var date = Date.now();
       var deviceData = Object.assign({ "partitionKey": Math.floor(date / (24 * 60 * 60 * 1000)) + '', "rowKey": date + '' + process.hrtime()[1] + '' }, iotHubMessage)  
       context.bindings.outputTable = deviceData;
       context.log(deviceData);
       context.done();
    };

数据会像这样

{ '0': 
   { gwName: 'Gateway-001',
     wellName: 'AI-10-81',
     wellId: 'BTG-110',
     pressure: 37.58,
     flow: 2.03,
     temperature: 110.96,
     rtc: 1,
     sdcard: 1,
     timestamp: 1517328108 },
  partitionKey: '17561',
  rowKey: '1517302524398738092399' }

是的,数据已经分开,但是出现了错误警告

Exception while executing function: Functions.storageiottrigger. Microsoft.Azure.WebJobs.Host: Error while handling parameter _binder after function returned:. Microsoft.WindowsAzure.Storage: Element 0 in the batch returned an unexpected response code.

我认为这部分与{ '0':有关,我不知道该部分的来源。需要一些帮助,谢谢。

0 个答案:

没有答案