有没有办法在 Spring Boot 中将反应式 cassandra 存储库与 Pageable 一起使用?

问题描述 投票:0回答:1

我正在尝试为我的存储库使用

ReactiveCassandraRepository
界面。当我尝试添加这样的
findAll
方法时:

public interface MatchRepository extends ReactiveCassandraRepository<Match, UUID> {
    Flux<Match> findAll(Pageable pageable);
}

我收到此错误消息:


Error creating bean with name 'matchRepository' defined in MatchRepository defined in 
@EnableReactiveCassandraRepositories declared on CassandraReactiveRepositoriesRegistrar.EnableReactiveCassandraRepositoriesConfiguration: 

Could not create query for public abstract reactor.core.publisher.Flux 
MatchRepository.findAll(org.springframework.data.domain.Pageable); 
Reason: No property 'findAll' found for type 'Match'

如何在 Spring Boot 中使用 Pageable 实现反应式 cassandra 存储库?有这方面的文档吗?

java spring-boot cassandra spring-webflux spring-data-cassandra
1个回答
0
投票

分页和响应式

Spring Boot Data Cassandra 不提供任何响应式分页功能,因为它确实有意义。 https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/reactive/package-summary.html

  • 在同步中,您正在等待页面作为结果集返回。
  • 在异步中,每页都有一个回调

在这两种情况下,当您耗尽结果集时,都会触发下一页的加载。

在反应式中,页面的概念没有意义,你会得到每条记录的回调/事件作为记录流/流量,没有开始也没有结束,因此页面没有意义。

可能的解决方案

现在要获取表中项目的子集,假设 100 个元素中有 15 到 25 个项目,有一个技巧称为

limit()
,但您必须使用
Flux<>
。您可能需要访问
CassandraOperations

int offset=15;
int pageSize=10;
ReactiveResultSet rs = cqlSession.executeReactive(bound);
Flux<ReactiveRow> myPage = Flux.from(rs).skip(offset).take(pageSize);

更多建议

  • 永远、永远不要在 Cassandra 表中执行

    findAll()
    。 Cassandra 旨在存储数十亿条记录。您正在对表进行完整扫描(非常慢),并且您可能永远无法在内存中处理它。

  • 在 Cassandra 中,表的设计基于查询/请求模式,并且始终有一个 where 子句。 where 子句将确定您如何对表进行分区。

© www.soinside.com 2019 - 2024. All rights reserved.