获取 "Entity already exists" 将聚合写入 Azure Table 存储时出错(使用 Azure 函数)

Getting "Entity already exists" error writing aggregates to Azure Table Storage (with Azure Function)

在 Azure 函数中,我试图聚合一些数据并将聚合写入 Table。

我有一个汇总数据的查询:

var query = recs
            .GroupBy(r => new { r.Category, r.Account, r.Record })
            .Select(r => new ts_webhitaggregate_account
                    {
                        PartitionKey = partition,  // Constant
                        RowKey = $"{r.Key.Category}:{r.Key.Account}:{r.Key.Record}", // Group By 
                        rawDate = intervaldate,   // Constant
                        epochDate = intervalepoch, // Constant
                        Category = r.Key.Category, // Group By 
                        Account = r.Key.Account, // Group By 
                        Record = r.Key.Record, // Group By 
                        Hits = r.Count(), // Aggregate
                        Users = r.Select(t => t.User).Distinct().Count(), // Aggregate
                        Devices = r.Select(t => t.Device).Distinct().Count() // Aggregate
                    });

然后我尝试将这些记录传递给 ICollector 绑定 Table

foreach (ts_webhitaggregate_account a in query.ToList())
{
    webhitsAggAccount.Add(a);
}

我经常遇到 "Entity already exists" 错误,例如:

Exception has been thrown by the target of an invocation. Microsoft.WindowsAzure.Storage: 82:The specified entity already exists.

如果我正在编写与 C# 相当的 SQL 语句,我不希望出现复合键的重复项,因为写入的每个值都是键、常量或聚合。我在 Table 中也没有可能导致冲突的预先存在的数据。

在查询中生成重复项我做错了什么?

错误信息告诉我们一个实体已经存在。据我所知,该集合似乎有一个具有相同 PartitionKey 和 RowKey 的实体。请尝试使用 insert or update method 看看它是否能给你帮助。

只是一个旁注,可能对您调试有帮助。在您粘贴的错误消息中“82:指定的实体已存在。”编号 82 是批处理操作中有问题(重复)项目的索引。

我相信我发现了我愚蠢的地方......这些发生在类别的循环中,我应该每次都选择一个类别,但行键上的一个范围选择包括另一个类别然后继续被选中,两次插入第二类。

如有疑问,请将所有内容打印到控制台!