Вот код для 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;
}
}