kudu 0.8.0版本支持scan来查询一个区域内的数据,目前不支持根据主键get api,可关注issueKUDU-1235,小米冰琳兄在做。
根据range查询
样例
KuduClient client = new KuduClient.KuduClientBuilder(KUDU_MASTER).build();
List<String> projectColumns = new ArrayList<String>(1);
projectColumns.add("id");
projectColumns.add("name");
projectColumns.add("age");
KuduTable table = client.openTable(tableName);
Schema schema = table.getSchema();
PartialRow partialRow = schema.newPartialRow();
partialRow.addInt("id", 1);
PartialRow partialRow1 = schema.newPartialRow();
partialRow1.addInt("id", 2);
ColumnRangePredicate crp = new
KuduScanner scanner = client.newScannerBuilder(table)
.setProjectedColumnNames(projectColumns)//指定输出列
.lowerBound(partialRow)//指定下限(包含)
.exclusiveUpperBound(partialRow1)//指定上限(不包含)
.build();
while (scanner.hasMoreRows()) {
RowResultIterator results = scanner.nextRows();
int i=0;
while (results.hasNext()) {
RowResult result = results.next();
System.out.println(i+" : "+result.getInt(0)+" "+result.getString(1)+" "+result.getInt(2));
}
break;
}
根据判断查询
样例
...
KuduPredicate kp = KuduPredicate.newComparisonPredicate(schema.getColumn("id"), KuduPredicate.ComparisonOp.EQUAL, 1);//创建predicate
KuduScanner scanner = client.newScannerBuilder(table)
.readMode(AsyncKuduScanner.ReadMode.READ_AT_SNAPSHOT)//设置读取快照模式
.setProjectedColumnNames(projectColumns)
.addPredicate(kp)//设置predicate
.snapshotTimestampMicros(Long.valueOf("1463388259000000"))//设置时间戳
.build();
...
注意,读取的是设置时间戳以前最近的数据。