Get the Azure table Row count

佐手、 提交于 2019-12-05 00:11:59

问题


I am new to Azure table storage. I want to get the total row count in a table.

Currently Iam doing something like this:-

public List<T> ReadAll(string partitionKey)
    {
        List<T> entities = new List<T>();

        TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey.ToLower()));
        entities = Table.ExecuteQuery(query).ToList();

        return entities;
    }

This currently reads through all the table entries. This works ok when it comes to less records. But I am concerned when the records would increase and this method would be tedious.

Is there any other elegant way to do it?


回答1:


Unfortunately there's no other way to do this. One thing you could possibly do is just fetch only few attributes only using query projection. What this will do is reduce the response payload and thus speed up the operation somewhat.

To elaborate my answer, as of today the only way to get the total count of entities is by fetching all entities. Your code above fetches all attributes for entities which is not really required because you're only interested in getting count of entities. That's why I said that you only fetch PartitionKey, RowKey, and Timestamp attributes only. To fetch only a subset of attributes, you could use query projection and specify a list of attributes you want to fetch (PartitionKey, RowKey, and Timestamp in our example). To do so, just modify your query to something like:

TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey.ToLower())).Select(new List<string> {"PartitionKey", "RowKey", "Timestamp"});



回答2:


Looks like you're doing it in C#, but as that's not specified in the question, here's how I did it in Powershell. You'll need Azure Powershell installed.

The important thing here is I'm telling it to only retrieve the PartitionKey column, though it still retrieves RowKey, Timestamp, and ETag as well, I couldn't figure out how to turn those off. If you don't override SelectColumns at all, or assign it an empty array, it'll retrieve all your table's columns, which you don't want here.

function GetTable($connectionString, $tableName)
{
    $context = New-AzureStorageContext -ConnectionString $connectionString
    $azureStorageTable = Get-AzureStorageTable $tableName -Context $context
    $azureStorageTable
}

function GetTableCount($table)
{
    #Create a table query.
    $query = New-Object Microsoft.WindowsAzure.Storage.Table.TableQuery

    #Define columns to select. 
    $list = New-Object System.Collections.Generic.List[string] 
    $list.Add("PartitionKey")

    #Set query details.
    $query.SelectColumns = $list

    #Execute the query.
    $entities = $table.CloudTable.ExecuteQuery($query)
    ($entities | measure).Count
}

$connectionString = "<yourConnectionString>"
$table = GetTable $connectionString <yourTableName>
GetTableCount $table

Hope this helps someone!



来源:https://stackoverflow.com/questions/28667601/get-the-azure-table-row-count

工具导航Map