弹簧数据cosmosdb库中不支持的cosmosdb的分页的抵消
我正在尝试通过分页从 cosmostemplate.pagination.query()
获取数据,但是问题是我没有从分页对象中设置的偏移中获取数据。以下是我用于设置分页的代码,
DocumentQuery documentQuery = new DocumentQuery(criteriaList, CriteriaType.AND));
if (Objects.nonNull(Offset) && Objects.nonNull(limit)) {
PageRequest cosmosPageRequest = CosmosPageRequest.of(Offset, limit);
documentQuery.with(cosmosPageRequest);
Page<User> page = cosmosTemplate.paginationQuery(documentQuery, User.class, COLLECTION_NAME);
...
}
这总是会返回使用第一组对象的列表。因此,例如,当我将偏移设置为11并限制10时,它总是将我的记录返回,偏移0到10。我也尝试查看库,但在检索记录时也没有设置偏移的位置。以下是同一形式的代码Azure-cosmosdb库 AbstractQueryGenerator.generatecosmosquery()
。
protected SqlQuerySpec generateCosmosQuery(@NonNull CosmosQuery query,
@NonNull String queryHead) {
final Pair<String, List<Pair<String, Object>>> queryBody = generateQueryBody(query);
String queryString = String.join(" ", queryHead, queryBody.getFirst(), generateQueryTail(query));
final List<Pair<String, Object>> parameters = queryBody.getSecond();
List<SqlParameter> sqlParameters = parameters.stream()
.map(p -> new SqlParameter("@" + p.getFirst(),
toCosmosDbValue(p.getSecond())))
.collect(Collectors.toList());
if (query.getLimit() > 0) {
queryString = new StringBuilder(queryString)
.append("OFFSET 0 LIMIT ")
.append(query.getLimit()).toString();
}
return new SqlQuerySpec(queryString, sqlParameters);
}
在此处也完成了偏移的硬编码,而不是从分页对象中取出。任何人都可以建议我在此库中不支持我做错了什么或根据偏移获得记录。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是Azure-Spring-Data-Cosmos SDK中的一个错误,它不符合
offset
作为cosmospagerequest
的一部分,并且始终将其设置为0。目前正在调查它,并将很快修复。遵循此问题以获取更新 - https://github.com/azure.com/azure/ Azure-sdk-for-java/essess/28032
然而,作为解决方法,最好的方法是使用本示例中提到的查询注释使用自定义查询 - offset的使用 -
查询注释 -
This is a bug in the azure-spring-data-cosmos SDK, where it does not honor the
OFFSET
as part ofCosmosPageRequest
and always set it to 0.It is currently being investigated and will be fixed soon. Follow this issue for updates - https://github.com/Azure/azure-sdk-for-java/issues/28032
However, as a workaround for now, the best way would be to use a custom query using query annotation as mentioned in this example - Usage of offset - https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ContactRepositoryIT.java#L235
Query annotation - https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/ContactRepository.java#L39