执行异步查询Azure表存储的最佳方法

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

//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()方法...我必须缺少一些东西。 谢谢您的帮助。


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

我已经转换了本教程的一些示例(如何使用.NET中的Table存储):

  • 创建一个表

    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();
    }
    
  • 将一个实体添加到表中

    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);
    
  • 插入一批实体

    // 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);
    
  • 等等...


  • 查看Azure存储团队博客文章中的Tables Deep Dive。 新的表服务层包括异步操作,如与异步等待模式一起工作的ExecuteQueryAsync(与同步ExecuteQuery方法相对应)。 另请阅读Storage Library 2.1发布文章,以获取有关使用新的表服务器层的IQueryable支持的更多信息。

    贾森

    链接地址: http://www.djcxy.com/p/77881.html

    上一篇: Best way to perform async query Azure Table Storage

    下一篇: Mongoose returns undefined for an existing field