Search code examples
pythonazureazure-cosmosdbazure-table-storageazure-cosmosdb-tables

How do I filter specific rows in Azure Tables using python SDK


I have data in a Azure storage table with the following schema:

PartitionKey      TimeStamp         Name
..                ...               ...

I want to return all entities with name starting with mem. I am trying to achieve this in python.

I could only find docs on doing a complete string comparison in filter parameter in the SDK (https://learn.microsoft.com/en-us/rest/api/storageservices/Querying-Tables-and-Entities?redirectedfrom=MSDN). Is there a way to perform a partial comparison?

The equivalent SQL query is

SELECT * from table where NAME LIKE mem%

Solution

  • There is no such operators in azure table storage.

    But there is a trick(here is a blog about this) you can use for your purpose. Define your $filter like below:

    "Name ge 'mem' and Name lt 'men'"
    

    Here is the test code(for more details, please refer to this doc), and works fine:

    from azure.cosmosdb.table.tableservice import TableService
    from azure.cosmosdb.table.models import Entity
    
    table_service = TableService(account_name='xxx', account_key='xxx')
    
    tasks = table_service.query_entities(
        'your_table_name', filter="Name ge 'mem' and Name lt 'men'")
    
    for task in tasks:
        print(task.Name)
    

    The test result at my side:

    enter image description here