简体   繁体   English

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

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

I've an Azure Function to perform some operations on the datasets in an Azure Table Storage.我有一个 Azure 函数来对 Azure 表存储中的数据集执行一些操作。

Since grouping is not working in Azure Table Storages, I have to fetch all datasets in my table and perform the operations I want (grouping, filtering) in my C# code.由于分组在 Azure 表存储中不起作用,我必须在我的表中获取所有数据集并在我的 C# 代码中执行我想要的操作(分组、过滤)。

But each query retrieves only the top 1000 datasets.但是每个查询只检索前 1000 个数据集。 How can I get all my datasets - or iterating over the table in bulks of 1000 to get all datasets at the end?我怎样才能得到我所有的数据集 - 或者批量迭代 1000 个表以在最后获取所有数据集?

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

var queryResult = myTable.ExecuteQuery(query);

When your query did not complete within certain limits (max. 5 seconds, max. 1000 rows, see here ), you'll receive a ContinuationToken in your result object.当您的查询未在特定限制内完成时(最多 5 秒,最多 1000 行,请参阅此处),您将在结果对象中收到一个ContinuationToken Pass this token to another query to continue your first query and get the next bunch of rows.将此令牌传递给另一个查询以继续您的第一个查询并获取下一组行。

This extension method does the job for you:此扩展方法为您完成工作:

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

Then you can use it in your code like然后你可以在你的代码中使用它

var queryResult = await myTable.ExecuteQueryAllElementsAsync(query);

You can't.你不能。 Probably to avoid long running query.可能是为了避免长时间运行的查询。

There are other limits wich you can read here: https://docs.microsoft.com/en-us/rest/api/storageservices/Query-Timeout-and-Pagination?redirectedfrom=MSDN您可以在此处阅读其他限制: 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