我已经实现了语义记录,将条目记录到azure表存储中。现在我想从azure表中删除旧的日志条目(比如说超过30天)。问题是azure表中的partitionKeys和RowKeys是动态生成的。像这样的东西
PartitionKey RowKey
2519569987199999999 MyProjectName_2519569788418593594_FFFFFFF8 //entry date 20 october 2015
2519573205599999999 MyProjectName_2519573006928838932_FFFFFFE1 // entry date 16 october 2015
我研究并发现PartitionKey的格式为DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks
如何从表中获取数据以进行删除?我需要进行全表扫描,因为partitionKey是动态的吗?
答案 0 :(得分:5)
如何从表中获取数据以进行删除?我需要做一个吗? 由于partitionKey是动态的全表扫描?
不,您不需要进行全表扫描。以下是如何找出30天之前创建的实体。
var today = DateTime.UtcNow.Date;
var thirtyDaysFromToday = today.AddDays(-30);
var pk = (DateTime.MaxValue.Ticks - thirtyDaysFromToday.Ticks).ToString();
var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
var tableClient = account.CreateCloudTableClient();
var table = tableClient.GetTableReference("YourTableName");
TableQuery query = new TableQuery().Where("PartitionKey ge '" + pk + "'").Select(new List<string>(){"PartitionKey", "RowKey"});//Since you only need PartitionKey/RowKey for entities deletion thus just fetch that only
TableContinuationToken token = null;
do
{
var queryResult = table.ExecuteQuerySegmented(query, token);
token = queryResult.ContinuationToken;
var entities = queryResult.Results;
//Now you can delete those entities
}
while (token != null);
注意:我没有测试过代码,所以请先尝试使用较小的数据集,然后再全力以赴:)