我目前正在用 Azure 的table存储把脚趾浸在水里。
我使用的是Azure.Data.Tables
而不是Microsoft.WindowsAzure.Storage.Table
,因为我可以读到它是较新的版本,将被维护。
所以现在我有了这个类,它有两个方法,一个用于upserting,另一个用于upserting许多实体:
public class TableRepository
{
private TableServiceClient _tableServiceClient;
private TableClient _tableClient;
public TableRepository(string key, string table)
{
_tableServiceClient = new TableServiceClient(key);
_tableClient = _tableServiceClient.GetTableClient(tableName: table);
}
public async Task AddAsync(TableEntity mapping)
{
await _tableClient.UpsertEntityAsync(mapping);
}
public async Task AddEntitiesToTableAsync(IEnumerable<TableEntity> mappings)
{
foreach (var m in mappings)
{
await _tableClient.UpsertEntityAsync(m);
}
}
}
对于我的批量upsert,这是工作,但对许多upsert是缓慢的,我想看看我是否可以批量upsert实体。
我发现对于Microsoft.WindowsAzure.Storage.Table
,我可以执行一批操作,并执行如下操作:
public async Task AddEntitiesToTableAsync(IEnumerable<EmailToIdMapping> mappings)
{
// Retrieve the CloudTable instance representing your Azure Storage table
CloudTable table = GetCloudTable(); // Replace with your own logic to get the CloudTable
// Create a List of TableOperation to hold the batch operations
List<TableOperation> batchOperations = new List<TableOperation>();
foreach (var m in mappings)
{
var tableEntity = new DynamicTableEntity(m.PartitionKey, m.RowKey)
{
Properties = new Dictionary<string, EntityProperty>
{
{ "Email", new EntityProperty(m.Email) },
{ "Id", new EntityProperty(m.Id) }
}
};
// Create an InsertOrReplace operation for the entity
TableOperation upsertOperation = TableOperation.InsertOrReplace(tableEntity);
batchOperations.Add(upsertOperation);
}
// Create a TableBatchOperation with the batch operations
TableBatchOperation batchOperation = new TableBatchOperation();
batchOperation.AddRange(batchOperations);
// Execute the batch operation
await table.ExecuteBatchAsync(batchOperation);
}
但是我找不到任何类似的Azure.Data.Tables
,我唯一能想到的就是找到一种更聪明的方法来做我已经在parralel中做的更有效的事情
我该怎么办?
1条答案
按热度按时间0lvr5msh1#
在
Azure.Data.Tables
中尝试SubmitTransactionAsync
方法请注意,为了成功,您应该有相同的分区键。否则,据我所知,没有你可以实现批量交易