Skip to content

Commit 136ea1e

Browse files
authored
Merge pull request #12 from aliyun/red-chen-patch-1
添加nextToken的样例
2 parents 7380b42 + 7a76cb9 commit 136ea1e

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/test/java/examples/OTSMultiDataSample.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,28 @@ private static void getRange(OTSClient client, String tableName)
154154
exclusiveEndKey.addPrimaryKeyColumn(COLUMN_UID_NAME,
155155
PrimaryKeyValue.fromLong(4)); // 范围的边界需要提供完整的PK,若查询的范围不涉及到某一列值的范围,则需要将该列设置为无穷大或者无穷小
156156

157-
criteria.setInclusiveStartPrimaryKey(inclusiveStartKey);
158-
criteria.setExclusiveEndPrimaryKey(exclusiveEndKey);
159157
GetRangeRequest request = new GetRangeRequest();
160-
request.setRangeRowQueryCriteria(criteria);
161-
GetRangeResult result = client.getRange(request);
162-
List<Row> rows = result.getRows();
158+
int consumedReadCU = 0;
159+
List<Row> rows = new ArrayList<Row>();
160+
161+
// 对表进行范围查询时,TableStore对一次查询返回的数据量会有限制,可能无法返回用户指定的范围内的所有数据。
162+
// TableStore的getRange接口返回的结果中会包含一个nextStartPrimaryKey,即当前已经扫描到的主键的断点。若
163+
// 需要继续在这个范围内读取还未返回的数据,需要拿这个nextStartPrimaryKey作为下一次查询的起始主键,继续下
164+
// 一次查询,直到查询返回的nextStartPrimaryKey为空为止。
165+
// 比如:用户想查询10条数据,通过调用一次GetRange,用户可能得到1条,5条,或者10条等,单次返回的结果是不确定
166+
// 的,要保证获取完整的数据,就需要使用到nextStartPrimaryKey,下面的例子展示了nextStartPrimaryKey的使用方式。
167+
168+
RowPrimaryKey next = inclusiveStartKey;
169+
do {
170+
criteria.setInclusiveStartPrimaryKey(next);
171+
criteria.setExclusiveEndPrimaryKey(exclusiveEndKey);
172+
request.setRangeRowQueryCriteria(criteria);
173+
GetRangeResult result = client.getRange(request);
174+
rows.addAll(result.getRows());
175+
next = result.getNextStartPrimaryKey();
176+
consumedReadCU += result.getConsumedCapacity().getCapacityUnit()
177+
.getReadCapacityUnit();
178+
} while (next != null);
163179

164180
System.out.println("GetRange result:");
165181
for (Row row : rows) {
@@ -172,9 +188,6 @@ private static void getRange(OTSClient client, String tableName)
172188
System.out
173189
.println("age信息为:" + row.getColumns().get(COLUMN_AGE_NAME));
174190
}
175-
176-
int consumedReadCU = result.getConsumedCapacity().getCapacityUnit()
177-
.getReadCapacityUnit();
178191
System.out.println("本次读操作消耗的读CapacityUnit为:" + consumedReadCU);
179192
}
180193

0 commit comments

Comments
 (0)