如何使用Azure.Data.Tables为Azure表创建批量upsert

jaxagkaj  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(102)

我目前正在用 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中做的更有效的事情
我该怎么办?

0lvr5msh

0lvr5msh1#

Azure.Data.Tables中尝试SubmitTransactionAsync方法

try
{
    var actions = new List<TableTransactionAction>();

    // Iterate over the entities and add up values
    foreach (var entity in entities)
    {
        var upsertAction = new UpsertEntityAction(entity);
        actions.Add(upsertAction);
    }

    var transaction = new TableTransaction(actions);
    var tableClient = client.GetTableClient("<pass-your-table-name-here>");

    // Execute as a batch transaction
    await tableClient.SubmitTransactionAsync(transaction);
}
catch (Exception e)
{
    throw;
}

请注意,为了成功,您应该有相同的分区键。否则,据我所知,没有你可以实现批量交易

相关问题