I am upgrading all my azure table storage functionality to Azure.Data.Tables library since the Microsoft.Azure.Cosmos.Table SDK is being deprecated.
I loaded batches of data easily using this code from the Cosmos package.
try {
var pks = entities.Select(x => x.PartitionKey).Distinct().ToList();
foreach (var pk in pks) {
await AddSymbolIfNotExistOrUpdateLastRecordDateAsync(pk, entities[0].RowKey);
var records = entities.Where(x => x.PartitionKey == pk).Distinct().ToList(); // partition key is the symbol
var batches = records.Batch(100); //batching max value is 100
completionMessage.Append($" A storage batch started for {records.Count} {pk} records into the table: {tableName} ");
#region BATCH JOB
//Iterating through each batch
foreach (var block in batches) {
var batchOperationObj = new TableBatchOperation();
foreach (var record in block) {
batchOperationObj.InsertOrReplace(record);
}
try {
var worked = await table?.ExecuteBatchAsync(batchOperationObj)! != null;
if (worked) {
batchResponse.NumberOfRecordsProcessed += batchOperationObj.Count;
batchOperationObj.Clear();
}
} // rest removed for brevity
I cannot find any methods, samples or tutorials that show how to insert batches using this SDK. Is it possible it is not doable using it? There are only Add methods for singular entities.
Any thoughts appreciated, as I load thousands of records at a time and just can't imagine doing via iteration is the only way to go.
It is certainly possible to do so. You will just need to use TableTransactionAction
to add an entity to the batch and then call TableClient.SubmitTransactionAsync
to save entities in the batch.
Here's the pseudo code:
TableClient client = new TableClient(new Uri(""));
var transactionActions = new List<TableTransactionAction>()
{
new TableTransactionAction(TableTransactionActionType.Add, new TableEntity("pk", "rk1")),
new TableTransactionAction(TableTransactionActionType.Add, new TableEntity("pk", "rk2")),
new TableTransactionAction(TableTransactionActionType.Add, new TableEntity("pk", "rk3")),
new TableTransactionAction(TableTransactionActionType.Add, new TableEntity("pk", "rk4")),
new TableTransactionAction(TableTransactionActionType.Add, new TableEntity("pk", "rk5")),
};
await client.SubmitTransactionAsync(transactionActions);