How to implement pagination using query_items_change_feed() in Azure Cosmos Python SDK?

Lekesh 0 Reputation points
2023-02-14T13:28:07.07+00:00

I am trying to extract historical data from a CosmosDB container using Azure Python SDK. I want to implement pagination when using query_items_change_feed(). I tried specifying max_item_count in the arguments. But it does not seem to follow the specified value. There are no proper documentation on this method for Python SDK.

Is it possible to implement pagination using Azure Python SDK? If yes, how do we implement it?

Code snippet:

Tested this on sample container with 1000 documents. This code created 20 json files with documents data in each files respectively. I notice that while uploading the 1000 documents to the container, it was uploaded like 60, 40, 39, 60, ... and JSON files sizes are also exactly in the same order. Why does this behaviour occur?

with cosmos_client.CosmosClient(self.HOST, self.MASTER_KEY) as client:
	db = client.get_database_client(database=DATABASE_ID)
	container = db.get_container_client(container=CONTAINER_ID)

	change_feed_iterator = container.query_items_change_feed(
		is_start_from_beginning=True,
		max_item_count=5,
		partition_key_range_id=0
	)
	i=1
	page = change_feed_iterator.__iter__()
	for item_page in change_feed_iterator.by_page():
		try:
			json.dump(list(item_page), open(f"batch_data/batch_data_test/_{i}.json", "w"), indent=4)
			i+=1
		except StopIteration:
			break
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,902 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. GeethaThatipatri-MSFT 29,542 Reputation points Microsoft Employee Moderator
    2023-02-17T16:17:23.0166667+00:00

    Hi, @Lekesh Welcome to the Microsoft Q&A platform, thanks for posting the question

    Looking at the code, I believe you'll need to call by_page() to create a paginated response. For example, client.query_items_change_feed(...).by_page()

    Regards

    Geetha


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.