本文主要是介绍Cassandra 分页 读取数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 为什么要分页
- 方案选择
- Token
- JPA
- PagingState
- 参考
为什么要分页
如果一个查询得到的记录数太大,一次性返回回来,那么效率非常低,并且很有可能造成内存溢出,使得整个应用都奔溃。所以,在数据量比较大的时候,分页还是非常有必要的。
方案选择
Token
Cassandra 提供了Token 函数 来记录上次查询的最后一条数据,但是它需要多个primary key, 不适合我们项目。所以放弃之。
JPA
看了下JPA API, 有setMaxResult 和 setFirstResult, 但是发下不work, 后来查了下,Cassandra对offset 还不support,所以这种方案不行。
Query query;List<Person> personList;// Create and execute SELECT * queryString cqlString = "Select p from Person p";// String cqlString = "Select p from Person p";query = entityManager.createQuery(cqlString).setMaxResults(100).setFirstResult(10);// This solution doesn't workfor (int i =0; i<10; i++) {personList = query.getResultList();// Verify number of records and contents are correctAssert.assertEquals(100, personList.size());System.out.println("haofan for: " + personList.get(10).getPersonId());}
PagingState
Cassandra 2.0 has auto paging. Instead of using token function to create paging, it is now a built-in feature.
PagingState: the driver exposes a PagingState object that represents where we were in the result set when the last page was fetched.(可以记录上次最后一页的位置)
public String paging(Session session, String pageState) {Statement statement = new SimpleStatement("SELECT * FROM testkeyspace.\"PERSON\"");statement.setFetchSize(3);if (pageState != null) {statement.setPagingState( PagingState.fromString(pageState));}ResultSet rs = session.execute(statement);int remaining = rs.getAvailableWithoutFetching();System.out.println("remaining " + remaining);for (Row row : rs) {System.out.println("first" + row);if (--remaining == 0) {break;}}return rs.getExecutionInfo().getPagingState().toString();
}
How to use this function:
@Test
public void pagingTest() throws Exception {Cluster cluster = Cluster.builder().addContactPoint("localhost").withPort(9042).build();Session session = cluster.connect();String pageState = paging(session, null);paging(session, pageState);
}
参考
- https://docs.datastax.com/en/developer/java-driver/3.6/manual/paging/
- https://stackoverflow.com/questions/26757287/results-pagination-in-cassandra-cql
这篇关于Cassandra 分页 读取数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!