【问题标题】:Spring Data Cassandra PaginationSpring Data Cassandra 分页
【发布时间】:2019-02-22 00:09:06
【问题描述】:

有人知道如何在 Spring Data Cassandra 中实现分页吗?

我已经尝试了所有可能的替代方案来为我的表格实现分页。 stackoverflow 的答案之一说它不是直接提供的。 Paging SELECT query results from Cassandra in Spring Boot application

根据文档 (https://docs.spring.io/spring-data/cassandra/docs/current-SNAPSHOT/reference/html/#cassandra.repositories.queries),CrudRepository 提供了 Pageable 方法。 org.springframework.data.repository.CrudRepository 不提供。

我正在使用 Cassandra 3.11.3 和 Spring Boot 1.5.1.RELEASE。 谁能给我一个简单的 Spring Data Cassandra 分页演示?

【问题讨论】:

  • 您始终可以构建您的自定义页面响应。会是这样的new PageImpl<>(entities, pageable, entitiesCount);
  • 您需要至少升级到 Spring Data for Apache Cassandra 2.0。
  • 如何使用 spring boot 2.x 和最新的 cassandra 驱动程序?

标签: spring-boot cassandra spring-data cassandra-3.0 spring-data-cassandra


【解决方案1】:

Spring Data Cassandra 中的分页以“仅向前”的方式工作,就像iterable 接口的工作方式一样。

    @Autowired
    PersonRepository personrepo;

    @GetMapping("/all/{page}")
    public List<Person> getPaginated(@PathVariable Integer page) {
        int currpage = 0, size = 2;
        Slice<Person> slice = personrepo.findAll(CassandraPageRequest.first(size));
        while(slice.hasNext() && currpage < page) {
            slice = personrepo.findAll(slice.nextPageable());
            currpage++;
        }

        return slice.getContent();
    }

您的存储库包含的位置:

public interface PersonRepo extends CrudRepository<Person, Integer> {

    Slice<Person> findAll(Pageable pr);

}

因此,您不断查询结果,直到到达所需的数据位置。

【讨论】:

    【解决方案2】:

    这是在响应式 Cassandra 或使用 spring 数据的常规 Cassandra 中进行分页的最简单方法

    在您的存储库类中创建带有查询注释的自定义方法(这里的关键点是第二种方法应该有一个需要作为参数发送的偏移量)假设创建日期是一个集群键
    用户表主键(user_id, date_created)

    例如:-

    @Query("select * from user order by date_created asc Limit :count)
    Flux<UserEntity> findFirstSetOfUsers(@Param(count) int noOfRecords);
    

    下一组记录

    @Query("select * from user where date_created > :dateCreated order by date_created asc Limit :count)
    Flux<UserEntity> findNextSetOfUsers(@Param(count) int noOfRecords, @Param(dateCreated) Date dateCreated);
    

    【讨论】:

      猜你喜欢
      • 2019-07-14
      • 2019-10-12
      • 2020-03-06
      • 2018-05-18
      • 2015-01-14
      • 2018-08-23
      • 2020-10-31
      • 2020-02-19
      • 1970-01-01
      相关资源
      最近更新 更多