Paginating Azure CosmosDB with Python SDK using continuation tokens

Viewed 756

I am trying to implement pagination using APIs with Azure Cosmos Python SDK. From what I have read and understand, we need continuation tokens. However, I cannot find any function in the SDK documentation here, that would consume the token and return the remaining data from the queries. My flow currently:

  • Initialize CosmosClient
  • Get database object
  • Get container object
  • Query the container, set max_count_size=1
  • Get Paged response, send it as a response to the API call

Now if I want the next page from the query, where do I pass the continuation token so that I can get the next page based on the previous query for API call?

from azure.cosmos import exceptions, CosmosClient, PartitionKey
endpoint = "https://xxxxxxxx.documents.azure.com:443/"
key = '===xxxx===xxxx===xxx'

client = CosmosClient(endpoint, key)
database_name = 'test'
database = client.create_database_if_not_exists(id=database_name)
container_name = 'FamilyContainer'
container = database.get_container_client(container_name)
query = "SELECT * FROM c "
items = container.query_items(
        query=query,
        max_item_count=1,
        enable_cross_partition_query=True
    )

pager = items.by_page()
first_page = list(pager.next())
print("first page: ", first_page )

Now, if I want the next page in another API call, where do I pass the continuation token? Azure SDK versions:

$ pip freeze | grep azure
azure-core==1.9.0
azure-cosmos==4.2.0
azure-nspkg==3.0.2
azure-storage-blob==12.6.0
azure-storage-nspkg==3.1.0
1 Answers
  • Here is an example on how to use it.

  • Here is the README file for the SDK, with lots of tips and valuable information like limitations.

Related