如何从表存储中检索1000多个结果?

时间:2019-03-05 19:54:23

标签: c# .net azure-table-storage

我们如何从表存储中检索1000多个结果?

当前,此方法检索的结果不超过1000个:

public static List<Translation> Get<T>(string sourceParty, string destinationParty, string wildcardSourceParty, string tableName) where T : ITableEntity, new()
{
    var acc = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("RosettaCacheStorageConnection"));
    var tableClient = acc.CreateCloudTableClient();
    var table = tableClient.GetTableReference(Environment.GetEnvironmentVariable("RosettaTableCache"));

    TableQuery<T> treanslationsQuery = new TableQuery<T>().Where(
        TableQuery.CombineFilters(
            TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition("sourceParty", QueryComparisons.Equal, sourceParty.ToLowerTrim()),
                TableOperators.And,
                TableQuery.GenerateFilterCondition("destinationParty", QueryComparisons.Equal, destinationParty.ToLowerTrim())
                ), TableOperators.Or,
            TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition("sourceParty", QueryComparisons.Equal, wildcardSourceParty),
                TableOperators.And,
                TableQuery.GenerateFilterCondition("destinationParty", QueryComparisons.Equal, destinationParty.ToLowerTrim()))
        ));

    TableContinuationToken continuationToken = null;
    Task<TableQuerySegment<T>> translations = table.ExecuteQuerySegmentedAsync(treanslationsQuery, continuationToken);
    TableQuerySegment<T> translationss = translations.Result;

    List<Translation> trans = translationss.Cast<Translation>().ToList();

    return trans.Where(x => x.expireAt > DateTime.Now)
                       .Where(x => x.effectiveAt < DateTime.Now)
                       .ToList();
}

是否可以检索1000多个结果?

请记住,这是Azure表存储,而不是cosmosdb。

2 个答案:

答案 0 :(得分:3)

我(至少很长一段时间没有使用过)Azure表存储,但是我怀疑问题是您只拨打了一个电话,传递了一个空的延续令牌,而忽略了结果。

为了获得所有结果,需要循环这段代码(最好是在异步方法中,等待任务):

TableContinuationToken continuationToken = null;
Task<TableQuerySegment<T>> translations = table.ExecuteQuerySegmentedAsync(treanslationsQuery, continuationToken);
TableQuerySegment<T> translationss = translations.Result;

我希望您需要循环,将 previous 结果中的ContinuationToken属性传递到ExecuteQuerySegmentedAsync方法中,直到继续标记为null表示结果结束。像这样:

var results = new List<T>();
TableContinuationToken continuationToken = null;
do
{
    var response = await table.ExecuteQuerySegmentedAsync(treanslationsQuery, continuationToken);
    continuationToken = response.ContinuationToken;
    results.AddRange(response.Results);
} while (continuationToken != null);

答案 1 :(得分:2)

默认情况下,由于在调用ExecuteQuerySegmentedAsync()时,Azure Table Storage在给定时间仅返回1000条记录,因此您选择获取1000条记录并使用continuationToken获取下一组记录。相反,您可以使用ExecuteQuery(query); 它将返回所有预期的记录。如果查看出站请求,您将看到ExecuteQuery正在通过发出多个请求直到返回所有记录来为您处理库中的延续。

更改此内容:

Task<TableQuerySegment<T>> translations = table.ExecuteQuerySegmentedAsync(treanslationsQuery, continuationToken);

为此:

var translations = table.ExecuteQueryAsync(treanslationsQuery);