【问题标题】:Best way to perform async query Azure Table Storage执行异步查询 Azure 表存储的最佳方式
【发布时间】:2014-04-03 03:59:02
【问题描述】:

我有一个使用 Windows Azure Storage Client 3.0 工作的基本 Azure 表存储查询。将其转换为异步查询的最简单方法是什么?是否可以使用异步等待模式?

//Setup the storage account connection
var cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
var cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
var table = cloudTableClient.GetTableReference("SampleTable");

//Get the context
var context = cloudTableClient.GetTableServiceContext();

//Setup the query
var q = from s in table.CreateQuery<SampleEntity>()
        where s.PartitionKey == sampleUID.ToString()
        select s;

//Get the list
var list = q.ToList();

插入和更新实体有 XyzAsync() 方法...我一定遗漏了一些东西。感谢您的帮助。

【问题讨论】:

标签: c# azure async-await azure-storage azure-table-storage


【解决方案1】:

最新版本的 SDK 现在支持异步 (nuget)。

您可以使用ExecuteSegmentedAsync 方法执行查询:

var query = (from s in table.CreateQuery<SampleEntity>()
            where s.PartitionKey == sampleUID.ToString() select s)
            .AsTableQuery<SampleEntity>();

TableContinuationToken continuationToken = null;
do
{
    // Execute the query async until there is no more result
    var queryResult = await query.ExecuteSegmentedAsync(continuationToken);
    foreach (var entity in queryResult)
    {

    }

    continuationToken = queryResult.ContinuationToken;
} while (continuationToken != null);

我已经转换了本教程的一些示例 (How to use Table storage from .NET):

  1. 创建表格

    async Task CreateATable()
    {
        // Retrieve the storage account from the connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
        // Create the table client.
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
        // Create the table if it doesn't exist.
        CloudTable table = tableClient.GetTableReference("people");
        await table.CreateIfNotExistsAsync();
    }
    
  2. 将实体添加到表中

    public class CustomerEntity : TableEntity
    {
        public CustomerEntity(string lastName, string firstName)
        {
            this.PartitionKey = lastName;
            this.RowKey = firstName;
        }
    
        public CustomerEntity() { }
    
        public string Email { get; set; }
    
        public string PhoneNumber { get; set; }
    }
    ...
    //The script:
    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    // Create the CloudTable object that represents the "people" table.
    CloudTable table = tableClient.GetTableReference("people");
    
    // Create a new customer entity.
    CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
    customer1.Email = "Walter@contoso.com";
    customer1.PhoneNumber = "425-555-0101";
    
    // Create the TableOperation object that inserts the customer entity.
    TableOperation insertOperation = TableOperation.Insert(customer1);
    
    // Execute the insert operation.
    await table.ExecuteAsync(insertOperation);
    
  3. 插入一批实体

    // Retrieve the storage account from the connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the table client.
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
    // Create the CloudTable object that represents the "people" table.
    CloudTable table = tableClient.GetTableReference("people");
    
    // Create the batch operation.
    TableBatchOperation batchOperation = new TableBatchOperation();
    
    // Create a customer entity and add it to the table.
    CustomerEntity customer1 = new CustomerEntity("Smith", "Jeff");
    customer1.Email = "Jeff@contoso.com";
    customer1.PhoneNumber = "425-555-0104";
    
    // Create another customer entity and add it to the table.
    CustomerEntity customer2 = new CustomerEntity("Smith", "Ben");
    customer2.Email = "Ben@contoso.com";
    customer2.PhoneNumber = "425-555-0102";
    
    // Add both customer entities to the batch insert operation.
    batchOperation.Insert(customer1);
    batchOperation.Insert(customer2);
    
    // Execute the batch operation.
    await table.ExecuteBatchAsync(batchOperation);
    
  4. 等等……

【讨论】:

  • 这个问题询问如何进行异步查询,然后发布查询代码。这个答案是关于异步操作的。这无关紧要。
【解决方案2】:

请查看以下来自 Azure 存储团队的博客文章 Tables Deep Dive。新的表服务层包括异步操作,例如 ExecuteQueryAsync(与同步 ExecuteQuery 方法相对应),它与异步等待模式一起使用。另请阅读Storage Library 2.1 Release 帖子,了解有关使用新表服务器层支持 IQueryable 的更多信息。

杰森

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-23
    • 2014-08-05
    • 1970-01-01
    • 2018-07-24
    • 2014-01-28
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    相关资源
    最近更新 更多