我正在使用Azure功能和基于Iot Hub事件中心的消息。消息通过JSON数组传入。我能够解析JSON数组没有问题,并获取消息EventData(获取DeviceId)和消息主体。当消息进入时,我想创建一个以DeviceId作为名称的表,分区键将是JSON数组中每个项的名称字段,然后我将使用“time”作为行键。这样我就可以从任何我想要的设备中获取数据,以及任何信号(由JSON对象中的“名称”字段表示)。然后我可以使用rowkey来获取两个日期之间的行。天青功能手动使用表格存储
我有现在的问题是,我得到一个错误
“CloudStorageAccount”这个名字在目前情况下
我读不存在,有一些版本问题为Microsoft.WindowsAzure.Storage
,但我不知道在哪里/如何解决它。
我的代码:
#r "Newtonsoft.Json"
#r "Microsoft.ServiceBus"
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.ServiceBus.Messaging;
using System;
public static void Run(EventData myEventHubMessage, TraceWriter log)
{
var CloudObject = new DeviceEventList();
var deviceId = GetDeviceId(myEventHubMessage);
var myMessageBody = GetPayload(myEventHubMessage.GetBytes());
CloudObject = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceEventList>(myMessageBody);
log.Info($"C# Event Hub trigger function processed a message: deviceId: {deviceId}");
var storageAccount = CloudStorageAccount.Parse(System.Environment.GetEnvironmentVariable("Storage", EnvironmentVariableTarget.Process));
var tableClient = storageAccount.CreateCloudTableClient();
var cloudTable = tableClient.GetTableReference(deviceId);
cloudTable.CreateIfNotExists();
foreach(var data in CloudObject.devicetelemetry)
{
log.Info($"C# Event Hub trigger function processed a message: name: {data.name}");
}
}
public class DeviceEventData
{
public string name{get;set;}
public string time{get;set;}
public string val{get;set;}
}
private static string GetPayload(byte[] body)
{
var json = System.Text.Encoding.UTF8.GetString(body);
return json;
}
private static string GetDeviceId(EventData message)
{
return message.SystemProperties["iothub-connection-device-id"].ToString();
}
public class DeviceEventList
{
public List<DeviceEventData> devicetelemetry {get;set;}
}
如果我注释掉下面几行,该函数解析消息,并打印出每个JSON对象的名称预期。
var storageAccount = CloudStorageAccount.Parse(System.Environment.GetEnvironmentVariable("Storage", EnvironmentVariableTarget.Process));
var tableClient = storageAccount.CreateCloudTableClient();
var cloudTable = tableClient.GetTableReference(deviceId);
cloudTable.CreateIfNotExists();
有没有什么特别的我可以用这种方式使用Azure存储表?我不认为我可以使用输出绑定到表,因为我需要基于deviceId动态表的名称。
感谢您的支持。它是固定的。我可以发誓我之前尝试过,但仍然出现错误。我会将其标记为答案。 –