繁体   English   中英

从 Azure 表存储中获取 1000 多个数据集

[英]Get more than 1000 datasets from an Azure Table Storage

我有一个 Azure 函数来对 Azure 表存储中的数据集执行一些操作。

由于分组在 Azure 表存储中不起作用,我必须在我的表中获取所有数据集并在我的 C# 代码中执行我想要的操作(分组、过滤)。

但是每个查询只检索前 1000 个数据集。 我怎样才能得到我所有的数据集 - 或者批量迭代 1000 个表以在最后获取所有数据集?

TableQuery<Models.product_item> query = new TableQuery<Models.product_item>()
          .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, myPartitionKey));

var queryResult = myTable.ExecuteQuery(query);

当您的查询未在特定限制内完成时(最多 5 秒,最多 1000 行,请参阅此处),您将在结果对象中收到一个ContinuationToken 将此令牌传递给另一个查询以继续您的第一个查询并获取下一组行。

此扩展方法为您完成工作:

public static class QueryExtensions
{
    public static async Task<IEnumerable<TElement>> ExecuteQueryAllElementsAsync<TElement>(this CloudTable table, TableQuery<TElement> tableQuery)
        where TElement : ITableEntity, new()
    {
        TableContinuationToken continuationToken = default(TableContinuationToken);
        var results = new List<TElement>(0);
        tableQuery.TakeCount = 500;

        do
        {
            //Execute the next query segment async.
            var queryResult = await table.ExecuteQuerySegmentedAsync(tableQuery, continuationToken);

            //Set exact results list capacity with result count.
            results.Capacity += queryResult.Results.Count;
            results.AddRange(queryResult.Results);

            continuationToken = queryResult.ContinuationToken;

        } while (continuationToken != null);

        return results;
    }
}

然后你可以在你的代码中使用它

var queryResult = await myTable.ExecuteQueryAllElementsAsync(query);

你不能。 可能是为了避免长时间运行的查询。

您可以在此处阅读其他限制: https ://docs.microsoft.com/en-us/rest/api/storageservices/Query-Timeout-and-Pagination?redirectedfrom =MSDN

暂无
暂无

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

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