简体   繁体   English

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

[英]How to retrieve more than 1000 results from table storage?

How can we retrieve more 1000 results from table storage? 我们如何从表存储中检索更多1000个结果?

Currently, this method is not retrieving more than 1000 results: 目前,此方法未检索超过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();
}

Is there a way to retrieve more 1000 results? 有没有办法检索更多1000个结果?

Please keep in mind that this is Azure Table Storage, and not cosmosdb. 请记住,这是Azure表存储,而不是cosmosdb。

I haven't used Azure Table Storage (at least not for a long time) but I suspect the problem is that you're only making a single call, passing in a null continuation token, and ignoring the continuation token in the result. 我没有使用过Azure Table Storage(至少很长时间没有使用过),但是我怀疑问题是您只在进行一次调用,传入一个空的延续令牌,而忽略了结果中的延续令牌。

This code (which would be better off in an async method, awaiting the task) needs to be looped in order to get all the results: 为了获得所有结果,需要循环这段代码(最好是在异步方法中,等待任务)。

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

I'd expect that you'd need to loop round, passing in the ContinuationToken property from the previous result into the ExecuteQuerySegmentedAsync method, until the continuation token is null to indicate the end of the results. 我希望您需要进行循环,将先前结果中的ContinuationToken属性传递到ExecuteQuerySegmentedAsync方法中,直到延续标记为null为止以指示结果结束。 So something like: 所以像这样:

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);

By default Azure Table Storage will only return 1000 records at a given time, since you are calling ExecuteQuerySegmentedAsync() you are choosing to take 1000 records and use the continuationToken to fetch next set of records. 默认情况下,由于在调用ExecuteQuerySegmentedAsync()时,Azure表存储将仅在给定时间返回1000条记录,因此您选择接收1000条记录并使用continuationToken获取下一组记录。 Instead you can use ExecuteQuery(query); 相反,您可以使用ExecuteQuery(query); which will return all records expected. 它将返回所有预期的记录。 If you look at the outbound requests you will see that ExecuteQuery is handling the continuations for you in the library by making the multiple requests until all records are returned. 如果查看出站请求,您将看到ExecuteQuery通过发出多个请求直到返回所有记录,正在为您在库中处理延续。

Change this: 更改此:

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

To this: 对此:

var translations = table.ExecuteQueryAsync(treanslationsQuery);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM