简体   繁体   中英

Spring boot 2.6 pagination with Cassandra

I need to implement pagination with Spring boot and Cassandra. I found cool solutionhere

However, it uses old version of Spring boot - 2.1.6.RELEASE , mine is 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.

But my version of Spring returns java.nio.ByteBuffer and I don't understand what I have to do with that.

Are there any ways to get String from this ByteBuffer or maybe other working ways to implement pagination?

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).

PSS I also tried to get bytes from ByteBuffer using byteBuffer.array() but it throws exception

That how it works in my case. Retrieving from 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 :

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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