joeabala
joeabala

Reputation: 266

Springboot cassandra pagination

I'm trying to achieve pagination with cassandra but I'm not getting any successful solutions on Stackoverflow. The prominent error I'm getting is 'Paging queries for pages other than the first one require a CassandraPageRequest with a valid paging state'. Kindly assist.

Upvotes: 2

Views: 2962

Answers (1)

RazvanParautiu
RazvanParautiu

Reputation: 2938

You must use the CassandraPageRequest.of(pageIndex, resultsOnPage) Also to be able to use findAll query with page you must extends your repository interface with CassandraRepositoy to have the implementation for the query.

Example :

  1. Declare a repository interface
        @Repository
        public interface MyRepository extends CassandraRepository<MyModel, MapId>{

        }
  1. Inject the repo and use the method findAll with pageable (you have to start with the page 0)
           @Autowired
            private MyRepo repo;

            void someMethod(){
             int resultOnPage = ...
             //this is the first page
             Slice<MyModel> page= repository.findAll(CassandraPageRequest.of(0, resultOnPage ));
             // iterate the slice with iterator
             //.......

             //go ahead and take the next pages
             while (page.hasNext()) {
               page = repository.findAll(page.nextPageable());
               //process the page iterating it
             }
           }

Upvotes: 4

Related Questions