我目前有一个查询,看起来类似于:
TableQuery<CloudTableEntity> query = new TableQuery<CloudTableEntity().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, PK));
foreach (CloudTableEntity entity in table.ExecuteQuery(query))
{
//Logic
}
我一直在研究并行,但是,我找不到任何关于如何使用它的好的代码示例。我希望能够查询数千个分区键,例如
CloudTableEntity().Where(PartitionKey == "11"|| PartitionKey == "22")
我可以拥有大约 40000 个分区键。有什么好的办法吗?
最佳答案
以下示例代码将并行发出多个分区键查询:
CloudTable table = tableClient.GetTableReference("xyztable");
List<string> pkList = new List<string>(); // Partition keys to query
pkList.Add("1");
pkList.Add("2");
pkList.Add("3");
Parallel.ForEach(
pkList,
//new ParallelOptions { MaxDegreeOfParallelism = 128 }, // optional: limit threads
pk => { ProcessQuery(table, pk); }
);
其中 ProcessQuery 定义为:
static void ProcessQuery(CloudTable table, string pk)
{
string pkFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, pk);
TableQuery<TableEntity> query = new TableQuery<TableEntity>().Where(pkFilter);
var list = table.ExecuteQuery(query).ToList();
foreach (TableEntity entity in list)
{
// Process Entities
}
}
请注意,在上面列出的同一查询中对两个分区键进行“或”操作将导致全表扫描。为了避免全表扫描,请按照上面的示例代码所示,为每个查询使用一个分区键执行各个查询。
有关查询构造的更多详细信息,请参阅 http://blogs.msdn.com/b/windowsazurestorage/archive/2010/11/06/how-to-get-most-out-of-windows-azure-tables.aspx
关于c# - 并行查询 Azure 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25575220/