【问题标题】:spring cassandra data Version 2.1.4 paginationspring cassandra data 2.1.4版分页
【发布时间】:2019-07-14 07:54:18
【问题描述】:

根据文档:https://docs.spring.io/spring-data/cassandra/docs/2.1.4.RELEASE/reference/html/#repositories.limit-query-result

Spring cassandra 数据使获取分页信息变得容易。 但我无法让它工作。

回购、调用和错误:

1.响应式呼叫

回购

public interface MyRepository extends ReactiveCassandraRepository<MyClass, String> {
  @Query("select * from my_keyspace.my_table where solr_query = ?0")
  Mono<Slice<MyClass>> findMono(String solrQuery, Pageable page);
}

致电:

 Mono<Slice<MyClass>>  repository.findMono(queryString, CassandraPageRequest.first(20));

错误:

"exceptionDescription":"org.springframework.core.codec.CodecException: 类型定义错误:[简单类型,类 com.datastax.driver.core.PagingState];嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:否 为类 com.datastax.driver.core.PagingState 找到序列化程序,但没有 发现创建 BeanSerializer 的属性(为了避免异常, 禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用 链: org.springframework.data.domain.SliceImpl[\"pageable\"]->org.springframework.data.cassandra.core.query.CassandraPageRequest[\"pagingState\"])","lines":["org.springframework .http.codec.json.AbstractJackson2Encoder.encodeValue(AbstractJackson2Encoder.java:175)","org.springframework.http.codec.json.AbstractJackson2Encoder.lambda$encode$0(AbstractJackson2Encoder.java:122)","re​​actor.core. publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100)","re​​actor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:67)","re​​actor.core.publisher.FluxMap$MapSubscriber.onNext( FluxMap.java:114)","re​​actor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onNext(FluxDefaultIfEmpty.java:92)","re​​actor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1476)", "reactor.core.publisher.MonoFlatMap$FlatMapInner.onNext(MonoFlatMap.java:241)","re​​actor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:121)","re​​actor.core.publisher. Operators$MonoSubscriber.complete(Operators.java:1476)","re​​actor.core.publisher.MonoCollectList$MonoBufferAllSubscriber.onComplete(MonoCollectList.java:118)","re​​actor.core.publisher.FluxTake$TakeFuseableSubscriber.onComplete(FluxTake. java:424)","re​​actor.core.publisher.FluxTake$TakeFuseableSubscriber.onNext(FluxTake.java:404)","re​​actor.core.publisher.FluxIterable$IterableSubscription.fastPath(FluxIterable.java:311)","re​​actor .core.publisher.FluxIterable$IterableSubscription.request(FluxIterable.java:198)"],

2。反应式与 ReactiveSortingRepository

回购:

public interface LocationRepository extends ReactiveSortingRepository<MyClass, String> {
}

致电:

 repository.findAll(CassandraPageRequest.first(20))

错误:

语法错误:findAll 不能应用于 CassandraPageRequest。

3.获取页面的简单调用。

回购:

public interface MyRepository extends CassandraRepository<MyClass, MyClassKey> {
Page<MyClass> findByKeyTerminalIdAndSolrQuery(String solrQuery, Pageable page);
}

启动时出错:

引起:org.springframework.dao.InvalidDataAccessApiUsageException: 不支持页面查询。使用切片查询。

4.使用 PagingAndSortingRepository

回购:

public interface MyRepository extends PagingAndSortingRepository<MyClass, MyClassKey> {

}

致电:

   Page<Vessel> vessels = repository.findAll(CassandraPageRequest.first(10));

错误:

springframework.data.mapping.PropertyReferenceException: 没有属性 找到类型 MyClass 的 findAll!

【问题讨论】:

    标签: java spring-boot cassandra pagination spring-data-cassandra


    【解决方案1】:

    欢迎来到 Stack Overflow。

    第一个例子是恰当的:

    public interface MyRepository extends ReactiveCassandraRepository<MyClass, String> {
      @Query("select * from my_keyspace.my_table where solr_query = ?0")
      Mono<Slice<MyClass>> findMono(String solrQuery, Pageable page);
    }
    
    Mono<Slice<MyClass>>  repository.findMono(queryString, CassandraPageRequest.first(20));
    

    问题在于,当您将 SliceImplSlice 的实现)传递给 WebFlux(根据堆栈跟踪)时,Jackson 无法对其进行编码。因此查询产生了正确的结果,但如果您想对其进行 JSON 编码,则需要传递 Slice 内容,而不是 Slice 本身。

    在相关说明中:ReactiveCassandraRepository 未实现 ReactiveSortingRepository,因为带有 Sort 参数的 Casandra 查询始终需要 WHERE 子句。查看ReactiveSortingRepository,您会看到一个不采用过滤条件的findAll(Sort) 方法:

    public interface ReactiveSortingRepository<T, ID> extends ReactiveCrudRepository<T, ID> {
        Flux<T> findAll(Sort sort);
    }
    

    【讨论】:

    • 感谢@mp911de 的回复。你是对的。错误是映射。但是我们需要 PageRequest 从偏移位置获取结果,而不仅仅是从 0 获取结果。当我使用:repository.findMono(queryString, CassandraPageRequest.of(20, 30)) 重试时,得到错误:java.lang.IllegalArgumentException: Cannot create a Cassandra page request for an indexed page other than the first page (0)。我们需要 repo 获取一个 CassandraPageRequest ,它与非零偏移位置一起使用,并向我们返回总行数。甚至 findAll 的 ReactiveSortingRepo 都无法满足要求。
    • 使用CassandraPageRequest.of(Pageable, PagingState)构造一个延续CassandraPageRequest
    • 非常感谢@mp911de 我们以前使用过分页状态。但是 Cassandra 分页有一些限制,比如它只能向前获取,我们必须获取通向我们需要的页面的所有页面。但这与spring数据无关。
    • 你找到方法了吗?
    猜你喜欢
    • 2019-02-22
    • 2019-10-12
    • 2020-03-06
    • 2018-05-18
    • 2015-01-14
    • 1970-01-01
    • 2018-08-23
    • 2020-10-31
    • 2020-02-19
    相关资源
    最近更新 更多