如何获取最新的1000 WADLogsTable条目?

时间:2016-03-01 11:31:35

标签: azure-storage azure-diagnostics wadslogtable

我编码用于从c#中的wadlogstable获取最新的诊断日志 但它正在遍历所有记录,然后提供最新的条目 恩。表中有5000条记录,但我只想要最新或最后1000条记录 但是它给出了所有记录,然后按查询顺序它给出了最后1000条记录,所以它非常耗时,花了将近7-8分钟来完成4000-5000条记录

 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ATCommon.DiagnosticConfig);
        CloudTableClient cloudTableClient = storageAccount.CreateCloudTableClient();
        TableServiceContext serviceContext = cloudTableClient.GetDataServiceContext();
        IQueryable<WadLogEntity> traceLogsTable = serviceContext.CreateQuery<WadLogEntity>("WADLogsTable");
        var selection = from row in traceLogsTable where row.PartitionKey.CompareTo("0" + DateTime.UtcNow.AddHours(hours).Ticks) >= 0 select row;
        //var selection = from row in traceLogsTable where row.PartitionKey.CompareTo("0" + DateTime.UtcNow.AddMinutes(-5.0).Ticks) >= 0 select row;
        CloudTableQuery<WadLogEntity> query = selection.AsTableServiceQuery<WadLogEntity>();
        IEnumerable<WadLogEntity> output = query.Execute();
return output.OrderByDescending(s => s.Timestamp).ToList();

2 个答案:

答案 0 :(得分:1)

您可以尝试通过技巧实现它,使用RowKey值 DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks ,允许您按照从最新项到较旧项的偏移时间对项目进行排序项目。通过执行此操作,您可以先按正确的顺序检索结果,或者最先添加的项目,然后使用 .Take(1000)参数将结果限制为最近的1000行。 查看this blog中的详细信息。

答案 1 :(得分:1)

我有超过5亿条目。每隔一秒它就会增加更多..

string apiLogTableName = "yourtableName";
StorageTable apiLogsTable = new StorageTable(apiLogTableName);

 string filter1 = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, date);   //hear you can check with ticks


 string filter2 = TableQuery.GenerateFilterConditionForInt("yourColumn", QueryComparisons.Equal, yourValue);

            string filter3 = TableQuery.GenerateFilterCondition("YourColumn2", QueryComparisons.NotEqual, "YourValue2");



            TableQuery<ApiCallLogEntity> findapiLogsRecord = new TableQuery<ApiCallLogEntity>().Where(TableQuery.CombineFilters(

                TableQuery.CombineFilters(
                            filter1,
                            TableOperators.And,
                            filter2),
                TableOperators.And, filter3));

//you can use
//var records= apiLogsTable._table.ExecuteQuery(findapiLogsRecord)


//bellow code will get one-one records // time consuming
            foreach (ApiCallLogEntity entity in apiLogsTable._table.ExecuteQuery(findapiLogsRecord))
            {
                Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
                    entity.Action, entity.RequestBody);


                tw.WriteLine("{0}, {1}\t{2}\t{3}\t{4}", entity.PartitionKey, entity.RowKey,
                    entity.Action, entity.RequestBody, entity.ResponseBody);
            }