簡體   English   中英

計算 Azure 表存儲中分區內的行數

[英]Count rows within partition in Azure table storage

我已經看到關於如何獲取 Azure 存儲表的總行數的各種問題,但我想知道如何獲取單個分區內的行數。

在將少量的實體數據加載到 memory 中時,我該如何做到這一點?

您可能已經知道,Azure 表中沒有類似Count功能。 為了獲得分區(或表)中實體(行)的總數,您必須獲取所有實體。

您可以使用稱為Query Projection的技術來減少響應負載。 查詢投影允許您指定希望表服務返回的實體屬性(列)列表。 由於您只對實體總數感興趣,我建議您只取回PartitionKey 您可能會發現此博客文章有助於理解查詢投影: https : //blogs.msdn.microsoft.com/windowsazurestorage/2011/09/15/windows-azure-tables-introducing-upsert-and-query-projection/

https://azure.microsoft.com/en-gb/features/storage-explorer/允許你定義一個查詢,你可以使用表統計工具欄項來獲取整個表或查詢的總行數

在此處輸入圖片說明

使用秒表測試了在除標准 TableEntity 之外還具有三個字段的 Partition 中獲取和計算 100,000 個實體的速度。

我只選擇 PartitionKey 並使用解析器得到一個字符串列表,一旦整個 Partition 被檢索,我就會計算。

我得到的最快的是大約6000ms - 6500ms 這是函數:

public static async Task<int> GetCountOfEntitiesInPartition(string tableName, string partitionKey)
    {
        CloudTable table = tableClient.GetTableReference(tableName);

        TableQuery<DynamicTableEntity> tableQuery = new TableQuery<DynamicTableEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)).Select(new string[] { "PartitionKey" });

        EntityResolver<string> resolver = (pk, rk, ts, props, etag) => props.ContainsKey("PartitionKey") ? props["PartitionKey"].StringValue : null;

        List<string> entities = new List<string>();

        TableContinuationToken continuationToken = null;
        do
        {
            TableQuerySegment<string> tableQueryResult =
                await table.ExecuteQuerySegmentedAsync(tableQuery, resolver, continuationToken);

            continuationToken = tableQueryResult.ContinuationToken;

            entities.AddRange(tableQueryResult.Results);
        } while (continuationToken != null);

        return entities.Count;
    }

這是一個通用函數,您只需要tableNamepartitionKey

您可以通過非常有效地利用 azure 表存儲服務的原子批處理操作來實現這一點。 對於每個分區,都有一個具有相同分區鍵和特定行鍵(如“PartitionCount”等)的附加實體。該實體將具有單個 int(或 long )屬性 Count。

每次插入新實體時,請執行原子批處理操作以增加分區計數器實體的 Count 屬性。 您的分區計數器實體將與您的數據實體具有相同的分區鍵,以便您可以在保證一致性的情況下執行原子批處理操作。

每次刪除實體時,請遞減分區計數器實體的 Count 屬性。 再次在批處理中執行操作,因此這兩個操作是一致的。

如果您只想讀取分區計數的值,那么您需要做的就是對分區計數器實體進行單點查詢,其 Count 屬性將告訴您該分區的當前計數。

這可以比@NickBrooks 的回答短一點。

public static async Task<int> GetCountOfEntitiesInPartition<T>(string tableName, string partitionKey) where T : ITableEntity, new()
{
    var tableClient = tableServiceClient.GetTableClient(tableName);
    var results = _tableClient.QueryAsync<T>(t => t.PartitionKey == partitionKey,
        select: new[] { "PartitionKey" });
    return await results.CountAsync();    
}

results.CountAsync()來自System.Linq.Asyncdo.net官方支持的 NuGet package。

我認為你可以直接在 C# 中使用.Count 您可以使用以下任一技術:

var tableStorageData = await table.ExecuteQuerySegmentedAsync(azQuery, null);
int count = tableStorageData.Count();

TableQuery<UserDetails> tableQuery = new TableQuery<UserDetails>();
var tableStorageData = table.ExecuteQuery(tableQuery,null);          
count = tableStorageData .Count();

count變量將具有取決於查詢的總行數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM