Hap*_*der 5 azure rowcount azure-table-storage
我是Azure表存储的新手.我想在表格中获得总行数.
目前Iam正在这样做: -
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;
}
Run Code Online (Sandbox Code Playgroud)
这当前读取所有表条目.当涉及到较少的记录时,这可以正常工作.但我担心记录会增加,这种方法会很乏味.
有没有其他优雅的方式来做到这一点?
Gau*_*tri 14
不幸的是,没有其他办法可以做到这一点.您可能做的一件事就是仅使用查询投影获取少量属性.这样做会减少响应有效负载,从而加快操作速度.
为了详细说明我的答案,截至今天,获取实体总数的唯一方法是获取所有实体.上面的代码获取了实际上不需要的实体的所有属性,因为您只对获取实体数量感兴趣.这就是我说你只获取PartitionKey, RowKey, and Timestamp属性的原因.要仅获取属性的子集,您可以使用query projection并指定要获取的属性列表(PartitionKey, RowKey, and Timestamp在我们的示例中).为此,只需将查询修改为:
TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey.ToLower())).Select(new List<string> {"PartitionKey", "RowKey", "Timestamp"});
Run Code Online (Sandbox Code Playgroud)
看起来你是用C#做的,但是问题中没有指出,这就是我在Powershell中的做法.您需要安装Azure Powershell.
这里重要的是我告诉它只检索PartitionKey列,虽然它仍然检索RowKey,Timestamp和ETag,我无法弄清楚如何关闭它们.如果你根本没有覆盖SelectColumns,或者为它指定一个空数组,它将检索你在这里不想要的所有表的列.
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
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人!
| 归档时间: |
|
| 查看次数: |
9263 次 |
| 最近记录: |