3

I am using the following code:

from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity

table_service = TableService(account_name='', account_key='')

task = table_service.get_entity('table1', '71cb50aa-75ac-4363-a14e-26df95c9b418', 'SCHEDULE')
print(task)

task2 = table_service.query_entities('table1', filter=None, select=None, num_results=1, marker=None, accept='application/json;odata=minimalmetadata', property_resolver=None, timeout=None)

print(task2)

But while writing python code, I do not know the partition key. I want to check if the latest entry into the table 'table1' contains 'SCHEDULE'.

New_to_work
  • 253
  • 2
  • 5
  • 16

1 Answers1

3

It sounds like you want to know how to use function query_entities(table_name, filter=None, select=None, num_results=None, marker=None, accept='application/json;odata=minimalmetadata', property_resolver=None, timeout=None) as the figure below to list the latest entites of the table table1 with SCHEDULE RowKey value.

enter image description here

As I known, you need to realize it with the filter parameter, as the code below.

filter = "RowKey eq 'SCHEDULE'
task2 = table_service.query_entities('table1', filter=filter)
print(list(task2))

The code above will list all entites with RowKey SCHEDULE and unknown PartitionKey, please refer to the offical document Querying Tables and Entities to know more details for using the filter parameter.

However, due to there is not support for sorting the entities by Timestamp, you can not get the latest entity directly by filter and num_results=1. If there is a pattern to know the timestamp range of the latest entities, you can try to use the code below.

import datetime
from operator import itemgetter

filter = "RowKey eq 'SCHEDULE' and Timestamp lt datetime'{}'".format(datetime.datetime.utcnow().isoformat()) # such as `2019-07-29T07:41:40.897643`
# Or filter = "RowKey eq 'SCHEDULE' and Timestamp ge datetime'<the start of datetime range>' and Timestamp lt datetime'<the end of datetime range>'"
task2 = table_service.query_entities('table1', filter=filter)
    print(list(task2))
newlist = sorted(task2, key=itemgetter('Timestamp'))
latest_entity = newlist[0]
print(latest_entity)

If you want a simple solution, I think Azure Function with Table Storage trigger will helps, please refer to the offical document Azure Table storage bindings for Azure Functions which supports Python.

Hope it helps.

Peter Pan
  • 23,476
  • 4
  • 25
  • 43