Azure CosmosDB 通过特定页码获取分页数据
Azure CosmosDB get pagination data by a specific page number
背景:FastAPI + CosmosDB
CosmosDB python SDK 的 official pagination code example 如下所示。它只展示了如何通过页面迭代器获取下一页。如何通过特定页码获取分页数据?
def test_paging_with_continuation_token(self):
created_collection = self.config.create_multi_partition_collection_with_custom_pk_if_not_exist(self.client)
document_definition = {'pk': 'pk', 'id': '1'}
created_collection.create_item(body=document_definition)
document_definition = {'pk': 'pk', 'id': '2'}
created_collection.create_item(body=document_definition)
query = 'SELECT * from c'
query_iterable = created_collection.query_items(
query=query,
partition_key='pk',
max_item_count=1
)
pager = query_iterable.by_page()
pager.next()
token = pager.continuation_token
second_page = list(pager.next())[0]
pager = query_iterable.by_page(token)
second_page_fetched_with_continuation_token = list(pager.next())[0]
self.assertEqual(second_page['id'], second_page_fetched_with_continuation_token['id'])
要按特定页码分页,您可以使用 Cosmos DB 中可用的 OFFSET LIMIT
子句 SQL API.
您需要做的就是在查询本身中指定 offset
和 limit
子句。类似于:
query = 'SELECT * from c OFFSET 1 LIMIT 1'
但是,请注意使用 OFFSET/LIMIT
会显着增加 RU 消耗。来自同一个 link:
The RU charge of a query with OFFSET LIMIT will increase as the number
of terms being offset increases. For queries that have multiple pages
of results, we typically recommend using continuation tokens.
Continuation tokens are a "bookmark" for the place where the query can
later resume. If you use OFFSET LIMIT, there is no "bookmark". If you
wanted to return the query's next page, you would have to start from
the beginning.
背景:FastAPI + CosmosDB
CosmosDB python SDK 的 official pagination code example 如下所示。它只展示了如何通过页面迭代器获取下一页。如何通过特定页码获取分页数据?
def test_paging_with_continuation_token(self): created_collection = self.config.create_multi_partition_collection_with_custom_pk_if_not_exist(self.client) document_definition = {'pk': 'pk', 'id': '1'} created_collection.create_item(body=document_definition) document_definition = {'pk': 'pk', 'id': '2'} created_collection.create_item(body=document_definition) query = 'SELECT * from c' query_iterable = created_collection.query_items( query=query, partition_key='pk', max_item_count=1 ) pager = query_iterable.by_page() pager.next() token = pager.continuation_token second_page = list(pager.next())[0] pager = query_iterable.by_page(token) second_page_fetched_with_continuation_token = list(pager.next())[0] self.assertEqual(second_page['id'], second_page_fetched_with_continuation_token['id'])
要按特定页码分页,您可以使用 Cosmos DB 中可用的 OFFSET LIMIT
子句 SQL API.
您需要做的就是在查询本身中指定 offset
和 limit
子句。类似于:
query = 'SELECT * from c OFFSET 1 LIMIT 1'
但是,请注意使用 OFFSET/LIMIT
会显着增加 RU 消耗。来自同一个 link:
The RU charge of a query with OFFSET LIMIT will increase as the number of terms being offset increases. For queries that have multiple pages of results, we typically recommend using continuation tokens. Continuation tokens are a "bookmark" for the place where the query can later resume. If you use OFFSET LIMIT, there is no "bookmark". If you wanted to return the query's next page, you would have to start from the beginning.