将 JSON 数据作为列存储在 Azure table 存储中

Storing JSON data as columns in Azure table storage

格式化我的 json 数据 and/or 如何更改我的函数,以便它作为列存储在 Azure table 存储中?

我正在向 IoT 中心发送 json 字符串:

{"ts":"2017-03-31T02:14:36.426Z","timeToConnect":"78","batLevel":"83.52","vbat":"3.94"}

我 运行 示例函数(在 Azure Function App 模块中)将数据从 IoT 中心传输到我的存储帐户:

    'use strict';

// This function is triggered each time a message is revieved in the IoTHub.
// The message payload is persisted in an Azure Storage Table
var moment = require('moment');

module.exports = function (context, iotHubMessage) {
   context.log('Message received: ' + JSON.stringify(iotHubMessage));
   context.bindings.deviceData = {
   "partitionKey": moment.utc().format('YYYYMMDD'),
      "rowKey": moment.utc().format('hhmmss') + process.hrtime()[1] + '',
      "message": JSON.stringify(iotHubMessage)
   };
   context.done();
};

但在我的存储 table 中,它显示为单个字符串,而不是分成几列(如存储资源管理器中所示。

如何将其放入 ts、timeToConnect、batLevel 和 vbat 的列中?

How do I get it into columns for ts, timeToConnect, batLevel, and vbat?

要在 table 中将这些属性作为单独的列获取,您需要取消对象并单独存储它们(目前您只是将整个对象转换为字符串并存储该字符串)。

请尝试以下代码:

module.exports = function (context, iotHubMessage) {
   context.log('Message received: ' + JSON.stringify(iotHubMessage));
   var deviceData = {
   "partitionKey": moment.utc().format('YYYYMMDD'),
      "rowKey": moment.utc().format('hhmmss') + process.hrtime()[1] + '',
   };
   Object.keys(iotHubMessage).forEach(function(key) {
     deviceData[key] = iotHubMessage[key];
   });
   context.bindings.deviceData = deviceData;
   context.done();
};

请注意,我没有尝试执行此代码,因此它可能包含一些错误。

如果有人正在寻找 c# 中的解决方案:

private static async Task ProcessMessage(string message, DateTime enqueuedTime)
{
    var deviceData = JsonConvert.DeserializeObject<JObject>(message);

    var dynamicTableEntity = new DynamicTableEntity();
    dynamicTableEntity.RowKey = enqueuedTime.ToString("yyyy-MM-dd HH:mm:ss.fff");

    foreach (KeyValuePair<string, JToken> keyValuePair in deviceData)
    {
        if (keyValuePair.Key.Equals("MyPartitionKey"))
        {
            dynamicTableEntity.PartitionKey = keyValuePair.Value.ToString();
        }
        else if (keyValuePair.Key.Equals("Timestamp")) // if you are using a parameter "Timestamp" it has to be stored in a column named differently because the column "Timestamp" will automatically be filled when adding a line to table storage
        {
            dynamicTableEntity.Properties.Add("MyTimestamp", EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
        }
        else
        {
            dynamicTableEntity.Properties.Add(keyValuePair.Key, EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
        }
    }

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("myStorageConnectionString");
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    CloudTable table = tableClient.GetTableReference("myTableName"); 
    table.CreateIfNotExists();

    var tableOperation = TableOperation.Insert(dynamicTableEntity);
    await table.ExecuteAsync(tableOperation);
}