簡體   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