【问题标题】:Azure function with table Storage - update entity not working带有表存储的 Azure 功能 - 更新实体不起作用
【发布时间】:2019-10-09 22:40:03
【问题描述】:

我有这个代码:

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;
public async static void Run(string myIoTHubMessage, ICollector<SensorData> tableOutput, CloudTable activityTable,ICollector<SensorData> activityValue, ILogger log)
{
    var data = JsonConvert.DeserializeObject<IoTMessage>(myIoTHubMessage);
    string temp =  data.param2;
    double temperature = double.Parse(temp) * 0.01;

    var sensor = new SensorData { Temperature = temperature.ToString(),DeviceId = data.deviceId, RowKey = data.messageId, PartitionKey = data.deviceId };
    tableOutput.Add(sensor);

    var query = new TableQuery<SensorValue>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, data.deviceId.ToString()));
    var segment = await activityTable.ExecuteQuerySegmentedAsync(query, null);

    if (segment.Results.Count == 0)
    {
        activityValue.Add(sensor);
    }
    else
    {
        SensorValue sensorValue = new SensorValue
        {
            PartitionKey = data.deviceId,
            RowKey = data.messageId,
            Temperature = Convert.ToInt16(temperature),
        };

        var operation = TableOperation.Replace(sensorValue);
        await activityTable.ExecuteAsync(operation);
        log.LogInformation(segment.Results.Count.ToString());
    }
}
public class SensorData
{
    public string RowKey { get; set; }
    public string PartitionKey { get; set; }
    public string Temperature { get; set; }
    public string DeviceId {get; set;}
}

public class IoTMessage
{
    public string messageId { get; set; }
    public string temperature { get; set; }
    public string deviceId { get; set; }

}

public class SensorValue : TableEntity
{
    public string RowKey { get; set; }
    public string PartitionKey { get; set; }
    public string Temperature { get; set; }
    public string DeviceId { get; set; }
}

该函数的工作方式是从传感器获取数据并将它们保存到基础中,但我想创建第二个表,其中仅保存给定设备的最后一个请求。我检查表中是否有一个设备没有添加这样的 id,它可以正常工作。不幸的是,我在更新给定记录时遇到了问题。在我看来是这样的:

   if (segment.Results.Count == 0)
        {
            activityValue.Add(sensor);
        }
        else
        {
            SensorValue sensorValue = new SensorValue
            {
                PartitionKey = data.deviceId,
                RowKey = data.messageId,
                Temperature = Convert.ToInt16(temperature),
            };

            var operation = TableOperation.Replace(sensorValue);
            await activityTable.ExecuteAsync(operation);
            log.LogInformation(segment.Results.Count.ToString());
        }

在activityTable表中没有这样的设备的情况下记录自己没有任何问题,不幸的是更新万一有这样的设备不起作用,即没有错误,但也没有记录更新:(

在我的例子中,在 activityTable 中,PartitionKey 到 deviceId 表的请求中发送。我试着从早上 8 点开始做,但没有任何效果,我无法处理它:(

【问题讨论】:

    标签: azure-functions azure-table-storage


    【解决方案1】:

    快速解决方案:将ETag 字段设置为*,如Azure 提示和技巧文章Updating an item from a Azure Storage Table 中所见。

    稍长的版本:根据表存储,您尝试替换的实体不存在,因为您省略了 ETag 值。

    ETag 属性用于更新期间的乐观并发。它不是时间戳,因为还有另一个名为 TimeStamp 的属性存储了上次更新记录的时间。例如,如果您加载一个实体并想要更新它,则 ETag 必须与当前存储的内容相匹配。如果您有多个用户编辑同一个项目,这一点很重要,您不希望他们覆盖彼此的更改。

    将 ETag 设置为 * 实际上是将其设置为接受任何值的通配符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      相关资源
      最近更新 更多