Reputation: 6166
In an Azure Function, I'm trying to aggregate some data and write the aggregations into a Table.
I have a query that summarises data:
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
});
I then attempt to pass these records to the ICollector bound Table
foreach (ts_webhitaggregate_account a in query.ToList())
{
webhitsAggAccount.Add(a);
}
I'm frequently encountering an "Entity already exists" error like:
Exception has been thrown by the target of an invocation. Microsoft.WindowsAzure.Storage: 82:The specified entity already exists.
If I was writing a comparable SQL statement to the C# I wouldn't expect duplicates of the compound key to be possible as every value being written is the key, a constant, or an aggregation. I also have no pre-existing data in the Table which could be causing the conflict.
What am I doing wrong to be generating duplicates in my query?
Upvotes: 2
Views: 6902
Reputation: 1526
Use "TableClient.UpsertEntity" : create if not exist and replace if exist
CallStatusEntity customerEntity = new CallStatusEntity()
{
PartitionKey = callerId,
RowKey = "",
IsRunning = true
};
tableClient.UpsertEntity(customerEntity);
Upvotes: 0
Reputation: 6166
I believe I found where I was being stupid ... these happen inside a loop for the category, and I should have been selecting a single category each time, but one range selection on the row key was including another category that then went on to be selected, twice inserted to the second category.
When in doubt, print everything to the console!
Upvotes: 1
Reputation: 3383
Just a side comment and may be helpful for you to debug. In the error message you pasted "82:The specified entity already exists." number 82 is the index of the problematic (duplicate) item in the batch operation.
Upvotes: 0
Reputation: 3293
The error message told us that an entity that already exists. As I know, the collection seems has an entity with the same PartitionKey and RowKey. Please try to use insert or update method to see whether it could give you help.
Upvotes: 0