Paging SELECT query results from Cassandra in Spring Boot application

允我心安 提交于 2019-12-24 07:57:07

问题


During my research I have come across this JIRA for Spring-Data-Cassandra: https://jira.spring.io/browse/DATACASS-56

Now, according to the post above, currently SDC is not supporting Pagination in the Spring App due to structure of Cassandra. However, I'm thinking, if I can pull the entire rows list into a Java List, can I Paginate that list ? I don't have much experience in Spring, but is there something I am missing when I assume this can be done ?


回答1:


Cassandra does not support pagination in the sense of pointing to a specific page (limit/offset) but generates a continuation token (PagingState) that is a set of bytes. Pulling a List of records will load all records in memory and possibly exhaust your memory (depending on the amount of data).

Spring Data Cassandra 1.5.0 RC1 comes with a streaming API in CassandraTemplate:

Iterator<Person> it = template.stream("SELECT * FROM person WHERE … ;", Person.class);

while(it.hasNext()) {
   // …
}

CassandraTemplate.stream(…) will return an Iterator that operates on an underlying ResultSet. The DataStax driver uses a configurable fetch-size (5000 rows by default) for bulk fetching. Streaming data access can fetch as much or as little data as you require to process data. Data is not retained by the driver nor Spring Data Cassandra, and once the fetched bulk is retrieved from the Iterator, the underlying ResultSet will fetch the next bulk itself.

The other alternative is using ResultSet directly that gives you access to PagingState and do all the continuation/paging business yourself. You would lose all the higher level benefits of Spring Data Cassandra.



来源:https://stackoverflow.com/questions/41396550/paging-select-query-results-from-cassandra-in-spring-boot-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!