public static async Task Run( [TimerTrigger("0 0 * * * *")] TimerInfo myTimer, [Table("AwaitConfirmationFormData")] CloudTable formDataTable, ILogger log) { log.LogInformation($"CleanUnconfirmedFormData: Checking for timestamps older than {DateTimeOffset.Now.AddDays(-1)}"); TableQuery <ContactFormEntity> rangeQuery = new TableQuery <ContactFormEntity>().Where( TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThan, DateTimeOffset.Now.AddDays(-1))); var result = await formDataTable.ExecuteQuerySegmentedAsync(rangeQuery, null); TableBatchOperation deleteBatch = new TableBatchOperation(); foreach (var item in result) { log.LogInformation($"CleanUnconfirmedFormData: Queue deletion of {item.Id} with timestamp {item.Timestamp}"); var deleteOperation = TableOperation.Delete(item); deleteBatch.Add(deleteOperation); } if (deleteBatch.Count() > 0) { log.LogInformation($"CleanUnconfirmedFormData: Deleting {deleteBatch.Count()} items"); await formDataTable.ExecuteBatchAsync(deleteBatch); } else { log.LogInformation($"CleanUnconfirmedFormData: No entries deleted"); } }
//INSERT operations public async void InsertOrReplaceBatch(List <T> entities) { TableBatchOperation batchCreateOperation = new TableBatchOperation(); int x = 0; foreach (T entity in entities) { x++; batchCreateOperation.Add(TableOperation.InsertOrReplace(entity)); if (batchCreateOperation.Count() >= 100 || x >= entities.Count) { await table.ExecuteBatchAsync(batchCreateOperation); batchCreateOperation.Clear(); } } }
public static async Task WriteToTableStorageAsync(CloudTableClient cloudTableClient, IEnumerable <T> items, string tableName) { var tableBatchOperation = new TableBatchOperation(); foreach (var item in items.Cast <IHashable>().ToList()) { item.PartitionKey = tableName; item.RowKey = item.CreateGuidFromSHA256Hash().ToString(); var operation = TableOperation.InsertOrReplace(item); tableBatchOperation.Add(operation); } if (tableBatchOperation.Count() > 0) { var table = cloudTableClient.GetTableReference(tableName); if (!await table.ExistsAsync()) { await table.CreateIfNotExistsAsync(); } await table.ExecuteBatchAsync(tableBatchOperation); } }