2

I am using python to make a query to Azure tables.

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

How can I see the amount of entities returned by this query? I have tried using

query.count
query.count()

but have had no luck. I get the following error.

'ListGenerator' object has no attribute 'count

Searching online keeps bring back results about getting the count of the entire rows in the table which is not relevant.

2 Answers 2

2

You should use len(query.items) to get the number of returned entities.

The code like below:

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

print(len(query.items))

Here is the test result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

There is a new SDK for the Azure tables service, you can install it from pip with the command pip install azure-data-tables. The new SDK can target either a storage account or a cosmos account. Here is a sample of how you can find the total number of entities in a table. You will have to iterate through each entity because the new Tables SDK uses paging on calls for query_entities and list_entities. Entities are turned in an ItemPaged which only returns a subset of the entities at a time.

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)

If you can clarify why you need the number of entities in a query I might be able to give better advice.

(Disclaimer, I work on the Azure SDK for Python team)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.