c# - 并行查询 Azure 存储

标签 c# azure azure-storage azure-table-storage

我目前有一个查询,看起来类似于:

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/

相关文章:

c# - 系统时钟调整会影响在 C# 中运行秒表吗?

c# - 从 C# 类库验证用户 - MVC

c# - 在从 C# 调用的 PowerShell Core 中安装 Az

Azure CloudBlobContainer.CreateIfNotExists() 在本地开发中抛出 Forbidden (403)

Azure 门户功能应用程序创建表单未列出现有存储帐户

c# - 遍历字典

c# - 如果使用Thread.Sleep进行检查,为什么秒表似乎不准确?

javascript - Azure 服务总线队列发送消息速度缓慢

azure - 设备授权授予类型的 Azure 资源管理范围有多大?

c# - 将数据引入 Azure 群集存储会出现 Kusto.Common.Svc.Exceptions.UnauthorizedOperationException 错误