简体   繁体   English

Spring Boot 2.6 使用 Cassandra 进行分页

[英]Spring boot 2.6 pagination with Cassandra

I need to implement pagination with Spring boot and Cassandra.我需要用 Spring boot 和 Cassandra 实现分页。 I found cool solutionhere我在这里找到了很酷的解决方案

However, it uses old version of Spring boot - 2.1.6.RELEASE , mine is 2.6.4 .但是,它使用旧版本的 Spring boot - 2.1.6.RELEASE ,我的是2.6.4 The main difference in my case is when I call pageRequest.getPagingState() with old version, it returns com.datastax.driver.core.PagingState object which I can map to String and return to a client and vice versa: I can get this object from a String value.我的情况的主要区别是当我用旧版本调用pageRequest.getPagingState()时,它返回com.datastax.driver.core.PagingState对象,我可以映射到String并返回到客户端,反之亦然:我可以得到这个来自 String 值的对象。

But my version of Spring returns java.nio.ByteBuffer and I don't understand what I have to do with that.但是我的 Spring 版本返回java.nio.ByteBuffer ,我不明白我要做什么。

Are there any ways to get String from this ByteBuffer or maybe other working ways to implement pagination?有没有办法从这个 ByteBuffer 中获取String或者其他工作方式来实现分页?

PS I know about solution with iterator, but don't think that keeping all objects in memory is a good idea (in case I have millions of objects). PS我知道使用迭代器的解决方案,但不要认为将所有对象保存在内存中是一个好主意(以防我有数百万个对象)。

PSS I also tried to get bytes from ByteBuffer using byteBuffer.array() but it throws exception PSS 我也尝试使用byteBuffer.array()从 ByteBuffer 获取字节,但它抛出异常

That how it works in my case.在我的情况下它是如何工作的。 Retrieving from String :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();
}

Mapping ByteBuffer to a String :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