[英]How to get more than 1000 entities from an azure table storage query?
[英]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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.