gpt4 book ai didi

azure - Golang/CosmosDB 分页

转载 作者:行者123 更新时间:2023-12-03 02:14:18 24 4
gpt4 key购买 nike

我正在尝试使用 cosmosapi package 从 CosmosDB 选择记录时实现分页。azure documentation声明延续 token 永不过期,我正在尝试理解其语义。

How does Cosmos DB Continuation Token work?有一项协议(protocol)是

Documents created after serving the first page are observable onsubsequent pages

我试图通过从 golang 应用程序运行一些实验来验证这一点,但有些事情不太正确。作为一个非常高级的示例,如果我们向 CosmosDB 插入三个记录:

Insert record #1
Insert record #2
Insert record #3

然后,如果我们尝试使用以下选项从表中进行选择(query = SELECT * FROM c ORDER BY c.dateField DESC):

opts := cosmosapi.QueryDocumentsOptions{
IsQuery: true,
ContentType: cosmosapi.QUERY_CONTENT_TYPE,
ConsistencyLevel: cosmosapi.ConsistencyLevelStrong,
Continuation: "",
PartitionKeyValue: partitionKeyValue,
MaxItemCount: 2,
}

它返回:

record #1
record #2
continuation token = "cont-token-1"

现在,当使用相同的选项再次选择但不同的继续标记时:

opts := cosmosapi.QueryDocumentsOptions{
IsQuery: true,
ContentType: cosmosapi.QUERY_CONTENT_TYPE,
ConsistencyLevel: cosmosapi.ConsistencyLevelStrong,
Continuation: "cont-token-1",
PartitionKeyValue: partitionKeyValue,
MaxItemCount: 2,
}

返回

record #3

这是相当合乎逻辑的。现在,当我尝试插入记录#4,并且它在记录#3之后插入,并尝试使用“cont-token-1”获取时,记录#4不会显示。仅当我通过使用空 opts.Continuation 字段再次选择来重新生成延续 token 时,它才会显示。

如果我尝试使用空的延续 token 进行选择,那么它会获取记录 #1 和记录 #2,并生成一个新的 token 来获取记录 #3 和记录 #4。

这是预期的行为吗?或者我错过了什么?根据我的理解,它应该出现。延续 token 就像一个书签,即使使用相同的延续 token ,它也应该看到结果。

最佳答案

连续 token 只能与完全相同的查询一起使用,并且每次都会返回完全相同的答案,无论您如何更改基础数据,如果您的基础数据以这种方式更改,您都需要获取新 token 这将包含在第一个答案中。

关于azure - Golang/CosmosDB 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71968746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com