2014-03-02 84 views
7

我有一個基本的Azure表存儲查詢使用Windows Azure存儲客戶端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()方法...我必須丟失一些東西。謝謝您的幫助。

+0

看看這個...我還沒有看在代碼一段時間,但我沒有很久以前與您的問題工作http://stackoverflow.com/a/13438313/328397 – LamonteCristo

回答

4

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 = "[email protected]"; 
    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 = "[email protected]"; 
    customer1.PhoneNumber = "425-555-0104"; 
    
    // Create another customer entity and add it to the table. 
    CustomerEntity customer2 = new CustomerEntity("Smith", "Ben"); 
    customer2.Email = "[email protected]"; 
    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. 等等...

+0

此問題詢問如何做一個異步查詢,然後張貼查詢的代碼。這個答案是關於異步操作。這是無關緊要的。 –

+0

@LéonPelletier,已更新 – Thomas

0

查看關於Azure存儲團隊博客文章的Tables Deep Dive。新的表服務層包括異步操作,如與異步等待模式一起工作的ExecuteQueryAsync(與同步ExecuteQuery方法相對應)。另請閱讀Storage Library 2.1 Release文章,瞭解有關使用新的表服務器層的IQueryable支持的更多信息。

傑森