2017-08-15 34 views
0

我正在使用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動態表的名稱。

回答

1

With #r "Microsoft.WindowsAzure.Storage"您正在添加對Microsoft.WindowsAzure.Storage的引用,但是,您不包括它。嘗試添加:

using Microsoft.WindowsAzure.Storage; 
+0

感謝您的支持。它是固定的。我可以發誓我之前嘗試過,但仍然出現錯誤。我會將其標記爲答案。 –