Azure功能-从IoT中心保存到表存储

时间:2019-05-13 20:32:03

标签: azure azure-functions azure-table-storage azure-iot-hub

我有5台设备连接到IoT中心。该设备发送消息,并且我已将此消息保存到Azure存储表,而不是blob。

不幸的是,我根据本指南https://blog.maximerouiller.com/post/persisting-iot-device-messages-into-cosmosdb-with-azure-functions-and-iot-hub/进行了所有操作,很遗憾,我可以添加输入和输出而没有任何问题,不幸的是,我无法处理编写将这些数据保存到表中的功能代码:(

我可以将数据从IoT中心保存到Blob存储,从具有流分析的IoT中心保存到表存储,但是如果没有SA,我无法从IoT中心保存到表存储:(

2 个答案:

答案 0 :(得分:1)

我建议您针对自己的情况使用azure table storage REST API。

您也可以为此使用SDK。请在下面看看。

课程

public class Item : TableEntity
    {
        public Item()
        {
            PartitionKey = "YourPartionKey";
            RowKey = "YourRowKey";
        }
        public string Message{ get; set; }
        public string Description { get; set; }

    }

使用SDK的内部功能

 Item entity = new Item("YourPartionKey", "YourRowKey")
            {
                Message= "I am From IOT Device",
                Description = "I am from IOT and Want to go to Storage"
            };
            // My Storage operation 
            var client = new CloudTableClient(new Uri("https://YourTableStorageAccountName.table.core.windows.net/"),
                      new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("YourTableStorageAccountName", "YourStorageKey"));
            var table = client.GetTableReference("YourTableName");

            TableOperation insertOperation = TableOperation.Insert(entity);
            var insertOnstorage = await table.ExecuteAsync(insertOperation);

            Console.WriteLine("Entity inserted!");

REST API参考

URL: https://YourAccount.table.core.windows.net/YourTableThatYouWantedToInsertMessase

Method: POST

Request Body:

{  

   "Message":"IOT Message",  
   "Description":"I am from IOT and Want to go to Storage",  
   "PartitionKey":"Yourpartitionkey",  
   "RowKey":"YourRowkey"  
}
  

注意:有关更多详细信息,请参见here

如果您还有其他查询,可以随时分享。谢谢,祝您编程愉快!

答案 1 :(得分:0)

以下是C#Azure Function V2的代码,该代码使用deviceId作为PartitionKey和messageId作为RowKey将数据保存到存储表中:

public static class IotHubToTableStorage
    {
        private static CloudTable _outputTable = CloudTableHelper.GetCloudTable("MyTableName");

        [FunctionName("IotHubToTableStorage")]
        public static async Task Run([EventHubTrigger("messages/events", Connection = "myConnectionString", ConsumerGroup = "myTablestorageConsumerGroup")]EventData eventData,
            ILogger log)
        {
            string message = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
            var deviceData = JsonConvert.DeserializeObject<JObject>(message);

            var dynamicTableEntity = new DynamicTableEntity();

            foreach (KeyValuePair<string, JToken> keyValuePair in deviceData)
            {
                if (keyValuePair.Key.Equals("deviceId"))
                {
                    dynamicTableEntity.PartitionKey = keyValuePair.Value.ToString();

                }
                else if (keyValuePair.Key.Equals("messageId"))
                {
                    dynamicTableEntity.RowKey = keyValuePair.Value.ToString();
                }
                else
                {
                    dynamicTableEntity.Properties.Add(keyValuePair.Key, EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));
                }
            }

            var tableOperation = TableOperation.InsertOrMerge(dynamicTableEntity);
            await _outputTable.ExecuteAsync(tableOperation);

        }
    }

它使用了此助手:

public class CloudTableHelper
        {
            public static CloudTable GetCloudTable(string tableName, string storageConnectionString)
            {
                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);

                // Create the table client.
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

                // Retrieve a reference to a table.
                CloudTable table = tableClient.GetTableReference(tableName);

                // Create the table if it doesn't already exist
                table.CreateIfNotExistsAsync().Wait();

                return table;
            }
        }