Golang / CosmosDB 分页

huangapple go评论92阅读模式
英文:

Golang / CosmosDB Pagination

问题

我正在尝试使用 cosmosapi 包 从 CosmosDB 中选择记录时实现分页功能。
Azure 文档 表示续订令牌永不过期,我正在尝试理解其语义。

https://stackoverflow.com/questions/63457763/how-does-cosmos-db-continuation-token-work 上有一个共识:

> 在提供第一页后创建的文档可以在后续页面上观察到

我尝试通过从 Golang 应用程序运行一些实验来验证这一点,但结果不太对。作为一个非常高级的示例,如果我们向 CosmosDB 插入三条记录:

插入记录 #1
插入记录 #2
插入记录 #3

然后,如果我们尝试使用以下选项从表中选择(查询 = 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,
}

它返回:

记录 #1
记录 #2 
续订令牌 = "cont-token-1"

现在,当再次使用相同的选项进行选择,但使用不同的续订令牌时:

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

它返回:

记录 #3

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

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

这是预期的行为吗?还是我漏掉了什么?
根据我的理解,它应该显示出来。续订令牌就像书签一样,即使使用相同的续订令牌,它也应该看到结果。

英文:

I'm trying to implement pagination while selecting records from CosmosDB using cosmosapi package.
The azure documentation states that continuation tokens never expire and I'm trying to understand the semantics of that.

In https://stackoverflow.com/questions/63457763/how-does-cosmos-db-continuation-token-work there is an agreement that

> Documents created after serving the first page are observable on
> subsequent pages

I tried to validate that point by running some experiments from a golang applicaiton, and something is not quite right. As a very high level example, if we insert three records to CosmosDB:

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

Then if we try to select from the table (query = SELECT * FROM c ORDER BY c.dateField DESC) using this options:

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

it returns:

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

Now when selecting again with the same options, but different continuation token:

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

It returns

record #3

Which is fairly logical.
Now when I try to insert record #4, and it gets inserted right after record #3, and try to fetch using "cont-token-1", record #4 does not show up. It only shows up when I regenerate the continuation tokens by selecting again using an empty opts.Continuation field.

If I try to select using an empty continuation token, then it fetches record #1 and record #2, and leads to a new token that fetches record #3 and record #4.

Is this the expected behavior? Or am I missing anything?
From my understanding, it should show up. The continuation token is like a bookmark, and it should see the results even when using the same continuation token.

答案1

得分: 1

一个继续令牌只能与完全相同的查询一起使用,并且每次都会返回完全相同的答案,无论您如何更改底层数据。如果您的底层数据发生了变化,而这些变化本应包含在第一个答案中,您需要获取一个新的令牌。

英文:

A continuation token can only be used with the exact same query and will return the exact same answer every time, regardless of how you change the underlying data, you need to get a new token if your underlying data changes in such a way that would have been included in the first answer.

huangapple
  • 本文由 发表于 2022年4月22日 20:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/71968746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定