如何获取 Azure 表查询中返回的实体数?

How to get number of entities returned in a Azure Tables query?

我正在使用 python 查询 Azure tables。

    query = table_service.query_entities(table_name, filter=filter_string)

如何查看此查询返回的实体数量?我试过使用

query.count
query.count()

但运气不佳。我收到以下错误。

'ListGenerator' object has no attribute 'count

在线搜索不断返回有关获取 table 中不相关的整行计数的结果。

您应该使用 len(query.items) 来获取返回实体的数量。

代码如下:

query = table_service.query_entities(table_name, filter=filter_string)

print(len(query.items))

测试结果如下:

Azure tables 服务有一个新的 SDK,您可以使用命令 pip install azure-data-tables 从 pip 安装它。新的 SDK 可以针对存储帐户或 cosmos 帐户。这是一个示例,说明如何找到 table 中的实体总数。您将不得不遍历每个实体,因为新的 Tables SDK 在调用 query_entitieslist_entities 时使用分页。实体在 ItemPaged 中上交,一次只有 returns 个实体的子集。

from azure.data.tables import TableClient, TableServiceClient

connection_string = "<your_conn_str>"
table_name = "<your_table_name>"

with TableClient.from_connection_string(connection_string, table_name) as table_client:

    f = "value gt 25"

    query = table_client.query_entities(filter=f)

    count = 0
    for entity in query:
        count += 1

    print(count)

如果您能阐明为什么需要查询中的实体数量,我可能会提供更好的建议。

(免责声明,我为 Python 团队开发 Azure SDK)