简体   繁体   中英

Storing JSON data as columns in Azure table storage

How do a format my json data and/or change my function so that it gets stored as columns in Azure table storage?

I am sending a json string to the IoT hub:

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

I run the sample function (in the Azure Function App module) to transfer the data from the IoT hub into my storage account:

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

But in my storage table, it shows up as a single string rather than getting split into columns (as seen in the storage explorer.

Azure存储资源管理器

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

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

To get these attributes as separate columns in table, you would need to defalte the object and store them separately (currently you are just converting the entire object into string and storing that string).

Please try the following code:

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

Please note that I have not tried to execute this code so it may contain some errors.

In case anyone is looking for a solution in 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM