Azure功能-从物联网中心保存到表存储

Azure功能-从物联网中心保存到表存储,azure,azure-functions,azure-table-storage,azure-iot-hub,Azure,Azure Functions,Azure Table Storage,Azure Iot Hub,我有5台设备连接到物联网集线器。此设备发送消息,我已将此消息保存到Azure存储表not blob 我根据本指南执行所有操作。不幸的是,我可以毫无问题地添加输入和输出。不幸的是,我无法编写将此数据保存到表中的函数代码:( 我可以将数据从物联网中心保存到Blob存储,从具有流分析的物联网中心保存到表存储,但如果没有SA,我无法将数据从物联网中心保存到表存储:(我建议您在您的案例中使用azure表存储REST API 你也可以使用SDK。请看下面 课程 公共类项:TableEntity { 公共项目

我有5台设备连接到物联网集线器。此设备发送消息,我已将此消息保存到Azure存储表not blob

我根据本指南执行所有操作。不幸的是,我可以毫无问题地添加输入和输出。不幸的是,我无法编写将此数据保存到表中的函数代码:(


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

我建议您在您的案例中使用
azure表存储
REST API

你也可以使用SDK。请看下面

课程

公共类项:TableEntity
{
公共项目()
{
PartitionKey=“您的PartitionKey”;
RowKey=“您的RowKey”;
}
公共字符串消息{get;set;}
公共字符串说明{get;set;}
}
使用SDK的内部函数

Item entity=新项(“YourPartionKey”、“YourRowKey”)
{
Message=“我来自物联网设备”,
Description=“我来自物联网,想去存储”
};
//我的存储操作
var client=新的CloudTableClient(新的Uri(“https://YourTableStorageAccountName.table.core.windows.net/"),
新的Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(“YourTablesStorage帐户名”、“YourStorageKey”);
var table=client.GetTableReference(“YourTableName”);
TableOperation insertOperation=TableOperation.Insert(实体);
var insertOnstorage=wait table.ExecuteAsync(insertOperation);
Console.WriteLine(“插入实体!”);
REST API参考

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

方法:
POST

请求正文:

{  

   "Message":"IOT Message",  
   "Description":"I am from IOT and Want to go to Storage",  
   "PartitionKey":"Yourpartitionkey",  
   "RowKey":"YourRowkey"  
}
注意:有关更多详细信息,请参考

如果您还有任何查询,请随意分享。谢谢您,编码愉快!

以下是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);

        }
    }

你的意思是你想使用azure函数在azure表存储中存储数据?你现在在哪里?@MdFaridUddinKiron我的意思是:-从温度传感器我向IoT Hub发送JSON消息,我想在没有流分析帮助的情况下使用azure函数将这些消息保存到azure表存储中,就像在lin中一样我在第一篇文章中提供的k。在我配置输入和输出的函数中,不幸的是,我完全停留在编写函数代码的层次上。在我给作者的链接中,我使用CosmoDB,我想写入表存储,他的代码需要2行,头部有问题,不幸的是,我无法处理它:(请提供您的表存储表结构,或者我为您准备一个演示如何使用函数插入表存储?@MdFaridUddinKiron我的请求如下:{“messageId”:1,“deviceId”:“Raspberry Pi”,“temperature”:20.617581550004086,“湿度”:67.09438874415045}实际上,对于我正在使用的这个任务,你是正确的,在C#中,你可以使用SDK和REST API。请查看答案,如果是解决你的问题,我的尝试将是成功的。谢谢,编码愉快!
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;
            }
        }