删除由语义记录创建的较旧的azure表存储日志条目

nit*_*igo 0 logging azure azure-storage semantic-logging

我已经实现了语义日志记录,以将条目记录到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
Run Code Online (Sandbox Code Playgroud)

我研究并发现PartitionKey的格式 DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks

如何从表中获取数据以进行删除?我需要进行全表扫描,因为partitionKey是动态的吗?

Gau*_*tri 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);
Run Code Online (Sandbox Code Playgroud)

注意:我没有测试过代码,所以请先尝试使用较小的数据集,然后再全力以赴:)