繁体   English   中英

Spring Boot 2.6 使用 Cassandra 进行分页

[英]Spring boot 2.6 pagination with Cassandra

我需要用 Spring boot 和 Cassandra 实现分页。 我在这里找到了很酷的解决方案

但是,它使用旧版本的 Spring boot - 2.1.6.RELEASE ,我的是2.6.4 我的情况的主要区别是当我用旧版本调用pageRequest.getPagingState()时,它返回com.datastax.driver.core.PagingState对象,我可以映射到String并返回到客户端,反之亦然:我可以得到这个来自 String 值的对象。

但是我的 Spring 版本返回java.nio.ByteBuffer ,我不明白我要做什么。

有没有办法从这个 ByteBuffer 中获取String或者其他工作方式来实现分页?

PS我知道使用迭代器的解决方案,但不要认为将所有对象保存在内存中是一个好主意(以防我有数百万个对象)。

PSS 我也尝试使用byteBuffer.array()从 ByteBuffer 获取字节,但它抛出异常

在我的情况下它是如何工作的。 String中检索:

@GetMapping("/test")
public Pageable test(@RequestParam Integer pageSize, @RequestParam String pagingState) {
    ByteBuffer byteBuffer = com.datastax.oss.protocol.internal.util.Bytes.fromHexString(pagingState);
    Pageable pageable = PageRequest.of(0, pageSize); // a page number might be any and the result depends only on paging state
    CassandraPageRequest pageRequest = CassandraPageRequest.of(pageable, byteBuffer);
    Slice<Test> all = repository.findAll(pageRequest1);
    return all.getPageable();
}

ByteBuffer映射到String

Slice<Test> tests = repository.findAll(pageRequest);
CassandraPageRequest cassandraPageRequest = (CassandraPageRequest) tests.getPageable();
ByteBuffer pagingState = cassandraPageRequest.getPagingState();
String pagingStateStr = com.datastax.oss.protocol.internal.util.Bytes.toHexString(pagingState);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM