如何将数据从服务总线存储到Azure表?

时间:2018-09-18 11:24:35

标签: azure azure-functions azure-table-storage

我想将通过主题接收的数据存储到Azure表。 我为此使用Azure函数。

public static class Function1
{
    [FunctionName("Function1")]
    public static async Task Run([ServiceBusTrigger("simple_test_topic", "stt_subscription", AccessRights.Manage, Connection = "ServiceBusConnectionString")]string mySbMsg,
        [Table("RoomBasicInformation")] CloudTable outputTable, TraceWriter log)
    {
        log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
        RoomEntry re = new RoomEntry(){ PartKey = "hpk", RoomId = "13", Temperature = "10"};
        var operation = TableOperation.Insert(re);
        await outputTable.ExecuteAsync(operation);
    }
}

public class RoomEntry : TableEntity
{
    public string PartKey { get; set; }
    public string RoomId { get; set; }
    public string Temperature { get; set; }
}

但是在Visual Studio中执行它后,我得到了

  

mscorlib:执行函数Function1时发生异常。   Microsoft.WindowsAzure.Storage:远程服务器返回错误:   (400)错误的请求。

1 个答案:

答案 0 :(得分:0)

您应该初始化PartitionKey的{​​{1}}和RowKey

Add an entity to a table

修改TableEntity以初始化这些值:

RoomEntity

您不需要public class RoomEntry : TableEntity { public RoomEntry() { } public RoomEntry(string partitionKey, string roomId) { this.PartitionKey = partitionKey; this.RowKey = roomId; } public string RoomId => this.RowKey; public string Temperature { get; set; } } 属性PartKey已有该属性。

还可以将Azure函数中的TableEntity初始化修改为:

RommEntity