本篇文章会介绍如何在表格存储上实现分页查询。
有别于传统关系型数据库支持的翻页查询,在NoSQL的数据模型和API上,实现分页功能会有以下的限制,需要特别注意:
- 不支持获取整个范围的行数,即无法计算总的页数。
- 不建议使用设置offset来跳页,因为offset的过滤是在客户端做,服务端不管如何,都会扫描读出那么多的数据。若offset设定过大,则可能导致本次查询时间过长。
- 最佳实践是只提供顺序翻页的功能,一页一页顺序往下翻。
下面是一个实现分页读接口的示例代码,提供offset过滤以及读取指定页数的数据:
/**
* 范围查询指定范围内的数据,返回指定页数大小的数据,并能根据offset跳过部分行。
*/
private static Pair<List<Row>, RowPrimaryKey> readByPage(OTSClient client, String tableName,
RowPrimaryKey startKey, RowPrimaryKey endKey, int offset, int pageSize) {
Preconditions.checkArgument(offset >= 0, "Offset should not be negative.");
Preconditions.checkArgument(pageSize > 0, "Page size should be greater than 0.");
List<Row> rows = new ArrayList<Row>(pageSize);
int limit = pageSize;
int skip = offset;
RowPrimaryKey nextStart = startKey;
// 若查询的数据量很大,则一次请求有可能不会返回所有的数据,我们需要流式的将所需要的数据全部查出来。
while (limit > 0 && nextStart != null) {
// 构造GetRange的查询参数。
// 注意:startPrimaryKey需要设置为上一次读到的位点,从上一次未读完的地方继续往下读,实现流式的范围查询。
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria(tableName);
criteria.setInclusiveStartPrimaryKey(nextStart);
criteria.setExclusiveEndPrimaryKey(endKey);
// 需要设置正确的limit,这里期望读出的数据行数最多就是完整的一页数据以及需要过滤(offset)的数据
criteria.setLimit(skip + limit);
GetRangeRequest request = new GetRangeRequest();
request.setRangeRowQueryCriteria(criteria);
GetRangeResult response = client.getRange(request);
for (Row row : response.getRows()) {
if (skip > 0) {
skip--; // 对于offset之前的数据,我们需要过滤掉,采用的策略是读出来后在客户端进行过滤。
} else {
rows.add(row);
limit--;
}
}
// 设置下一次查询的起始位点
nextStart = response.getNextStartPrimaryKey();
}
return new Pair<List<Row>, RowPrimaryKey>(rows, nextStart);
}
下面是使用以上接口,顺序的一页一页读取某个指定范围内的所有数据的例子:
private static void readByPage(OTSClient client, String tableName) {
int pageSize = 8;
int offset = 33;
RowPrimaryKey startKey = new RowPrimaryKey();
startKey.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.INF_MIN);
startKey.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.INF_MIN);
RowPrimaryKey endKey = new RowPrimaryKey();
endKey.addPrimaryKeyColumn(COLUMN_GID_NAME, PrimaryKeyValue.INF_MAX);
endKey.addPrimaryKeyColumn(COLUMN_UID_NAME, PrimaryKeyValue.INF_MAX);
// 读第一页,从范围的offset=33的行开始读起
Pair<List<Row>, RowPrimaryKey> result = readByPage(client, tableName, startKey, endKey, offset, pageSize);
for (Row row : result.getKey()) {
System.out.println(row.getColumns());
}
System.out.println("Total rows count: " + result.getKey().size());
// 顺序翻页,读完范围内的所有数据
startKey = result.getValue();
while (startKey != null) {
System.out.println("============= start read next page ==============");
result = readByPage(client, tableName, startKey, endKey, 0, pageSize);
for (Row row : result.getKey()) {
System.out.println(row.getColumns());
}
startKey = result.getValue();
System.out.println("Total rows count: " + result.getKey().size());
}
}
完整的例子可以从这里下载。
时间: 2024-08-31 02:28:43