Scan
获取全部数据 @Test
//测试性能
public void test() throws IOException {
Scan scan=new Scan();
excuteScan(scan);
}
可以设置setbatch
@Test
//测试性能
public void test() throws IOException {
Scan scan=new Scan();
//Set the maximum number of values to return for each call to next()
scan.setBatch(4);
excuteScan(scan);
}
源码
控制的是每次next调用的数量,那继续看next
/**
* Set the maximum number of values to return for each call to next()
* @param batch the maximum number of values
*/
public Scan setBatch(int batch) {
if (this.hasFilter() && this.filter.hasFilterRow()) {
throw new IncompatibleFilterException(
"Cannot set batch on a scan using a filter" +
" that returns true for filter.hasFilterRow");
}
this.batch = batch;
return this;
}
@Override
public Result next() throws IOException {
// If the scanner is closed and there's nothing left in the cache, next is a no-op.
if (cache.size() == 0 && this.closed) {
return null;
}
// 缓冲中没有就 RPC 调用读取数据进缓存
if (cache.size() == 0) {
loadCache();
}
// 缓冲中有直接从缓存中取
if (cache.size() > 0) {
return cache.poll();
}
// if we exhausted this scanner before calling close, write out the scan metrics
writeScanMetrics();
return null;
}
每次从缓存 cache 中读,缓存为空则 loadCache , 实际上 cache 是通过一个链表来实现的.
setBatch() 坑爹的命名,这个实际上是配置获取的列数,假如表有两个列簇 cf,info,每个列簇5个列。这样每行可能有10列了,setBatch() 可以控制每次获取的最大列数,进一步从列级别控制流量。配置建议:当列数很多,数据量大时考虑配置此参数,例如100列每次只获取50列。一般情况可以默认值(-1 不受限)。
Get
看几个例子就明白了
/**
* 单行获取每次RPC请求值发送一个Get对象中的数据,因为Get对象初始化时需要输入行键,因此可以理解为一个Get对象就代表一行。
* 一行中可以包含多个列簇或者多个列等信息
*
* @throws IOException
*/
@Test
//利用get查询数据
public void testget() throws IOException {
Get get = new Get("rk000010".getBytes());
get.setClosestRowBefore(true);
System.out.println(get);
//如何从geg对象中获取数据
//Extracts certain cells from a given row.
Result result = table.get(get);
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
System.out.println(new String(CellUtil.cloneRow(cell)) + "\t" + new String(CellUtil.cloneValue(cell)));
}
}
@Test
//利用get查询数据
public void testget1() throws IOException {
Get get = new Get("rk000010".getBytes());
//如何从geg对象中获取数据
//Extracts certain cells from a given row.
/**
* (1)Result对象,在查询得到的结果,每一行数据会被作为一个Result对象,将数据存入到一个Result实例中。
* 当我们需要获取一行数据时则需要获取该行数据所在的Result对象即可。该对象内部封装了一个KeyValue 对象数组。
* 在0.98.4以前的本班。result类提供了 raw() 方法去获取整个result对象中的KeyValue数组。
* 在0.98.4以后,则提供了一个新的节后: rowCells() 方法获取KeyValue对象,不过返回的是KeyValue 对象父类引用
*/
Result result = table.get(get);
System.out.println(get.getMaxVersions());
//Return an cells of a Result as an array of KeyValues WARNING do not use, expensive.
KeyValue[] kvs = result.raw();
System.out.println(kvs.length);//4
for (KeyValue kv : kvs) {
System.out.println(Bytes.toString(kv.getRow()));
System.out.println(Bytes.toString(kv.getValue()));
}
}