我在卡桑德拉有一张有一百万张唱片的桌子。我想一次提取100条记录,所以如果我提取前100条,下一次提取应该从项目101开始。我怎样才能得到这种分页呢?我也使用了PagingState
,但它不起作用。
我的代码如下:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.datastax.driver.core.PagingState;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
/**
*
* The solution of skipping rows is that use page state rather than iterator
* rows one by one.
*
*/
public class CassandraPaging {
private Session session;
public CassandraPaging(Session session) {
this.session = session;
}
/**
* Retrieve rows for the specified page offset.
*
* @param statement
* @param start
* starting row (>1), inclusive
* @param size
* the maximum rows need to retrieve.
* @return List<Row>
*/
public List<Row> fetchRowsWithPage(Statement statement, int start, int size) {
ResultSet result = skipRows(statement, start, size);
return getRows(result, start, size);
}
private ResultSet skipRows(Statement statement, int start, int size) {
ResultSet result = null;
int skippingPages = getPageNumber(start, size);
String savingPageState = null;
statement.setFetchSize(size);
boolean isEnd = false;
for (int i = 0; i < skippingPages; i++) {
if (null != savingPageState) {
statement = statement.setPagingState(PagingState
.fromString(savingPageState));
}
result = session.execute(statement);
PagingState pagingState = result.getExecutionInfo()
.getPagingState();
if (null != pagingState) {
savingPageState = result.getExecutionInfo().getPagingState()
.toString();
}
if (result.isFullyFetched() && null == pagingState) {
// if hit the end more than once, then nothing to return,
// otherwise, mark the isEnd to 'true'
if (true == isEnd) {
return null;
} else {
isEnd = true;
}
}
}
return result;
}
private int getPageNumber(int start, int size) {
if (start < 1) {
throw new IllegalArgumentException(
"Starting row need to be larger than 1");
}
int page = 1;
if (start > size) {
page = (start - 1) / size + 1;
}
return page;
}
private List<Row> getRows(ResultSet result, int start, int size) {
List<Row> rows = new ArrayList<>(size);
if (null == result) {
return rows;
}
int skippingRows = (start - 1) % size;
int index = 0;
for (Iterator<Row> iter = result.iterator(); iter.hasNext()
&& rows.size() < size;) {
Row row = iter.next();
if (index >= skippingRows) {
rows.add(row);
}
index++;
}
return rows;
}
}
这是主要的方法:
public static void main(String[] args) {
Cluster cluster = null;
Session session = null;
try {
cluster = Cluster.builder().addContactPoint("localhost").withPort(9042).build();
session = cluster.connect("mykeyspace");
Statement select = QueryBuilder.select().all().from("mykeyspace", "Mytable");
CassandraPaging cassandraPaging = new CassandraPaging(session);
System.out.println("*************First Page1 **************");
List<Row> firstPageRows = cassandraPaging.fetchRowsWithPage(select, 1, 5);
printUser(firstPageRows);
System.out.println("*************Second Page2 **************");
List<Row> secondPageRows = cassandraPaging.fetchRowsWithPage(select, 6, 5);
printUser(secondPageRows);
System.out.println("*************Third Page3 **************");
List<Row> thirdPageRows = cassandraPaging.fetchRowsWithPage(select, 6, 5);
printUser(thirdPageRows);
cluster.close();
session.close();
} catch(Exception exp) {
exp.printStackTrace();
} finally {
cluster.close();
session.close();
}
}
private static void printUser(final List<Row> inRows) {
for (Row row : inRows) {
System.out.println("Id is:" + row.getUUID("id"));
System.out.println("Name is:" + row.getInt("name"));
System.out.println("account is:" + row.getString("account"));
}
}
最佳答案
要使用以下解决方案,您需要在类路径中使用spring数据依赖项。
spring提供了PageRequest
这是Pageable
的一个实现,accepspageNo
和size
(页面上没有要显示的记录)。
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
PageRequest(int page, int size)
例子
创建回购协议。
要创建回购协议,请使用
org.springframework.data.repository.PagingAndSortingRepository
class CasandraRepo extends PagingAndSortingRepository{
}
//在
pageReq
中使用此repository.findAll
,如下所示;Pageable pageReq = new PageRequest(0, 10);
CasandraRepo repo;
repo.findAll(pageReq);
关于java - Cassandra 分页,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44254428/