将JSON数据存储为Azure表存储中的列

问题描述 投票:6回答:2

如何格式化我的json数据和/或更改我的功能,以便将其存储为Azure表存储中的列?

我正在向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();
};

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

Azure storage explorer

如何将其插入ts,timeToConnect,batLevel和vbat的列中?

json azure azure-table-storage
2个回答
2
投票

如何将其插入ts,timeToConnect,batLevel和vbat的列中?

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

请尝试以下代码:

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

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


0
投票

如果有人在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);
}
© www.soinside.com 2019 - 2024. All rights reserved.