Best way to perform async query Azure Table Storage
I have a basic Azure table storage query working using the Windows Azure Storage Client 3.0. What's the easiest way to convert this to an async query? Is it possible to use the async await pattern?
//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();
Inserting and updating entities have XyzAsync() methods... I must be missing something. Thanks for the help.
Latest versions of the SDk now support async (nuget).
You can execute your query using the ExecuteSegmentedAsync Method :
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);
I've converted some sample of this tutorial (How to use Table storage from .NET):
Create a 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();
}
Add an entity to a table
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);
Insert a batch of entities
// 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);
And so on...
Take a look at the Tables Deep Dive following blog posts from the Azure Storage team. The new Table Service Layer includes async operations such as ExecuteQueryAsync (counterpart to the sync ExecuteQuery method) which works with the async await pattern. Also read the Storage Library 2.1 Release post for more information on IQueryable support using the new Table Server Layer.
Jason
链接地址: http://www.djcxy.com/p/77882.html上一篇: 如何在文本文件中编写一个numpy矩阵
下一篇: 执行异步查询Azure表存储的最佳方法