1
votes

I have 5 devices connected to IoT Hub. This device send message and i have save this message to Azure Storage Table not blob.

I do everything based on this guide https://blog.maximerouiller.com/post/persisting-iot-device-messages-into-cosmosdb-with-azure-functions-and-iot-hub/ unfortunately I can add input and output without any problems , unfortunately, I can not deal with writing a function code which will save this data to the table :(

I can save data from IoT Hub to Blob Storage, from IoT Hub with Stream Analytics to Table storage, but i can't save from IoT Hub without SA to table storage :(

2
You mean you would like to store data in azure table storage using azure function? Where are you up to now?Md Farid Uddin Kiron
@MdFaridUddinKiron I mean something like: - From temperature sensors I send JSON messages to IoT Hub, and I would like to save these messages to Azure Table Storage without the help of Stream Analytics, and using Azure Function, as in the link that I provided in the first post. In the function I configured input and output, unfortunately I got stuck completely at the level of writing the function code. In the link that I gave the author uses CosmoDB, I would like to write to Table Storage, his code takes 2 lines and a problem with the head, unfortunately I can not handle it :(PawelC
Will you provide your table storage table structure , Or I would prepare a demo for you how you could insert on table storage using function?Md Farid Uddin Kiron
@MdFaridUddinKiron My request looks like this: {"messageId":1,"deviceId":"Raspberry Pi","temperature":20.617581550004086,"humidity":67.09438874415045} actualy for this task i'm using azure-samples.github.io/raspberry-pi-web-simulatorPawelC
Right you are, in C# you can use both SDK and REST API for that. Please take a look at the answer, If it is resolve your issue my attempt would be success. Thanks and happy coding!Md Farid Uddin Kiron

2 Answers

1
votes

I would suggest you to use azure table storage REST API for your case.

You could also Use SDK for that. Please take a look below.

Class

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

    }

Inside Function Using 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 Reference

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"  
}

Note: For more details you could refer here

If you have any more query feel free to share. Thank you and happy coding!

0
votes

Here is the code for an C# Azure Function V2 that saves your data to a storage table using the deviceId as PartitionKey and the messageId as 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);

        }
    }

It makes use of this Helper:

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