在上次实训中我们是这样获取数据的:
Get get = new Get(Bytes.toBytes("row1"));//定义get对象
Result result = table.get(get);//通过table对象获取数据
那么问题来了,我们想要获取多条数据,比如说查询1万条数据怎么办呢?
可能我们第一时间就会想到循环,例如:
String tableName = "test";
Table table = connection.getTable( TableName.valueOf(tableName));// 获取表
for (String rowkey : rowkeyList){
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
for (Cell kv : result.rawCells()) {
String value = Bytes.toString(CellUtil.cloneValue(kv));
list.add(value);
}
}
这样做是非常低效的,如果有10000
条数据那我们需要发送10000
次请求,这样非常耗时,如果在自己本机上尝试,查询时间可能在5
分钟左右。
这样肯定不行,我们在HBase
的Table
对象和子类的源码中找找看有没有解决办法,忽然眼前一亮:
public Result[] get(List<Get> gets) throws IOException {
if (gets.si