【发布时间】:2015-09-01 15:57:03
【问题描述】:
Azure 存储表都有一个时间戳列。根据文档here,列出的从存储表中删除的方法是选择一个实体,然后将其删除。
有谁知道如何使用代码根据时间戳值的日期时间比较从存储表中删除任何实体?
编辑:
根据给出的建议,我编写了以下代码。但是,它会在我的 table.ExecuteQuery(rangeQuery) 调用中引发错误请求异常。有什么建议吗?
StorageCredentials creds = new StorageCredentials(logAccountName, logAccountKey);
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudTableClient client = account.CreateCloudTableClient();
CloudTable table = client.GetTableReference(LogTable);
TableQuery<CloudQuerySummary> rangeQuery = new TableQuery<CloudQuerySummary>()
.Where(TableQuery.GenerateFilterCondition("Timestamp", QueryComparisons.LessThan
, DateTime.Now.AddHours(- DateTime.Now.Hour).ToString()));
TableOperation deleteOperation;
// Loop through the results, displaying information about the entity.
foreach (CloudQuerySummary entity in table.ExecuteQuery(rangeQuery))
{
deleteOperation = TableOperation.Delete(entity);
table.Execute(deleteOperation);
}
编辑 2
这是任何选择复制/引用它的人的最终工作代码。
public void DeleteLogsNotFromToday()
{
StorageCredentials creds = new StorageCredentials(logAccountName, logAccountKey);
CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
CloudTableClient client = account.CreateCloudTableClient();
CloudTable table = client.GetTableReference(LogTable);
TableQuery<CloudQuerySummary> rangeQuery = new TableQuery<CloudQuerySummary>()
.Where(TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThan
, DateTime.Now.AddHours(-DateTime.Now.Hour)));
try
{
TableOperation deleteOperation;
// Loop through the results, displaying information about the entity.
foreach (CloudQuerySummary entity in table.ExecuteQuery(rangeQuery))
{
deleteOperation = TableOperation.Delete(entity);
table.Execute(deleteOperation);
}
}
catch (Exception ex)
{
throw;
}
}
【问题讨论】:
-
Great =) 记得按分区键分组,1..100个批量删除,降低交易成本。
标签: c# azure azure-table-storage