How to get all rows in Azure table Storage in C#?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 04:24:24

问题


I am trying to get a list of all entities inside an azure table.

Any idea of how I would write this query?


回答1:


To answer your question, you could do something like the following:

var acc = new CloudStorageAccount(
                         new StorageCredentials("account name", "account key"), true);
var tableClient = acc.CreateCloudTableClient();
var table = tableClient.GetTableReference("table name");
var entities = table.ExecuteQuery(new TableQuery<MyEntity>()).ToList();

However please keep in mind that table service returns a maximum of 1000 entities in a single call to it. If there're more than 1000 entities available in your table, it returns a continuation token which can be used to fetch next set of entities. The ExecuteQuery method actually handles this continuation token internally thus if you want to cancel this operation for any reason, you can't do that.

A better approach would be to use ExecuteQuerySegmented method and have your application deal with the token. Here's the sample code to do so:

var acc = new CloudStorageAccount(
                         new StorageCredentials("account name", "account key"), true);
var tableClient = acc.CreateCloudTableClient();
var table = tableClient.GetTableReference("table name");
TableContinuationToken token = null;
var entities = new List<MyEntity>();
do
{
    var queryResult = table.ExecuteQuerySegmented(new TableQuery<MyEntity>(), token);
    entities.AddRange(queryResult.Results);
    token = queryResult.ContinuationToken;
} while (token != null);



回答2:


A possibly more efficient way of lazily retrieving items from the table is this:

    public IEnumerable<T> GetAll<T>(string tableName) where T : class
    {
        var table = this.GetCloudTable(tableName);
        TableContinuationToken token = null;
        do
        {
            var q = new TableQuery<T>();
            var queryResult = Task.Run(() => table.ExecuteQuerySegmentedAsync(q, token)).GetAwaiter().GetResult();
            foreach (var item in queryResult.Results)
            {
                yield return item;
            }
            token = queryResult.ContinuationToken;
        } while (token != null);
    }

If the caller is looping through the result of GetAll and find what they were looking for, they could just break the loop, and the GetAll method would stop retrieving next items. This might be more efficient, though this wouldn't make much difference if you really had to retrieve all items.


If using C# 8.0 you can yield inside async methods:

    public async Task<IEnumerable<T>> GetAll<T>(string tableName) where T : class
    {
        var table = this.GetCloudTable(tableName);
        TableContinuationToken token = null;
        do
        {
            var q = new TableQuery<T>();
            var queryResult = await table.ExecuteQuerySegmentedAsync(q, token);
            foreach (var item in queryResult.Results)
            {
                yield return item;
            }
            token = queryResult.ContinuationToken;
        } while (token != null);
    }


来源:https://stackoverflow.com/questions/23940246/how-to-get-all-rows-in-azure-table-storage-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!