gpt4 book ai didi

c# - 执行异步查询 Azure 表存储的最佳方式

转载 作者:太空狗 更新时间:2023-10-29 17:50:43 24 4
gpt4 key购买 nike

我有一个使用 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() 方法...我一定错过了一些东西。感谢您的帮助。

最佳答案

最新版本的 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b2c1a170f1e093b1814150f14081455181416" rel="noreferrer noopener nofollow">[email protected]</a>";
    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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e240b08082e0d01001a011d01400d0103" rel="noreferrer noopener nofollow">[email protected]</a>";
    customer1.PhoneNumber = "425-555-0104";

    // Create another customer entity and add it to the table.
    CustomerEntity customer2 = new CustomerEntity("Smith", "Ben");
    customer2.Email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="286a4d46684b47465c475b47064b4745" rel="noreferrer noopener nofollow">[email protected]</a>";
    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. 等等...

关于c# - 执行异步查询 Azure 表存储的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22124144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com