HBase根据Rowkey批量查询数据JAVA API(一次查多条,返回多个记录)


最近在生产中遇到了一个需求,前台给我多个rowkey的List,要在hbase中查询多个记录(返回给前台list)。在网上也查了很多,不过自己都不太满意,filter的功能有可能查询结果不是准确值,而网上给出的get方法也都是返回一条,scan的话都是返回全部数据,还有用rowkey范围查询的,都跟我的这个应用场景不符啊。无奈,自己找了一个方法,给各位有同样需求的朋友们一个参考。


首先创建链接属性:

  1. public static Configuration conf = null;
  2. public static Connection connection = null;
  3. public static Admin admin = null;
  4. static {
  5. conf = HBaseConfiguration.create();
  6. conf.set( "hbase.zookeeper.quorum", "10.120.193.800,10.120.193.810");
  7. conf.set( "hbase.master", "10.120.193.730:60010");
  8. conf.set( "hbase.zookeeper.property.clientPort", "2181");
  9. conf.set( "zookeeper.znode.parent", "/hbase-unsecure");
  10. conf.set( "hbase.client.keyvalue.maxsize", "1048576000"); //1G
  11. conf = HBaseConfiguration.create(conf);
  12. try {
  13. connection = ConnectionFactory.createConnection(conf);
  14. admin = connection.getAdmin();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }

紧接着,开始做查询操作:

  1. public static List<String> qurryTableTest(List<String> rowkeyList) throws IOException {
  2. String tableName = "table_a";
  3. Table table = connection.getTable( TableName.valueOf(tableName)); // 获取表
  4. for (String rowkey : rowkeyList){
  5. Get get = new Get(Bytes.toBytes(rowkey));
  6. Result result = table.get(get);
  7. for (Cell kv : result.rawCells()) {
  8. String value = Bytes.toString(CellUtil.cloneValue(kv));
  9. list.add(value);
  10. }
  11. }
  12. return list;
  13. }

但是!!!重点来了,每条rowkey都要发起一次请求,这种方法效率十分低,测试了10000条记录查询要用4分钟左右,项目需求是秒回啊,这怎么能行?

于是,就自己点开hbase java 源码,自己慢慢找,忽然眼前一亮!在HTable.class里看到了这个get方法:

  1. public Result[] get(List<Get> gets) throws IOException {
  2. if(gets.size() == 1) {
  3. return new Result[]{ this.get((Get)gets.get( 0))};
  4. } else {
  5. try {
  6. Object[] r1 = this.batch(gets);
  7. Result[] results = new Result[r1.length];
  8. int i = 0;
  9. Object[] arr$ = r1;
  10. int len$ = r1.length;
  11. for( int i$ = 0; i$ < len$; ++i$) {
  12. Object o = arr$[i$];
  13. results[i++] = (Result)o;
  14. }
  15. return results;
  16. } catch (InterruptedException var9) {
  17. throw (InterruptedIOException)( new InterruptedIOException()).initCause(var9);
  18. }
  19. }
  20. }

这就简单了,紧接着对自己上边的方法进行改进:

  1. public static List<String> qurryTableTestBatch(List<String> rowkeyList) throws IOException {
  2. List<Get> getList = new ArrayList();
  3. String tableName = "table_a";
  4. Table table = connection.getTable( TableName.valueOf(tableName)); // 获取表
  5. for (String rowkey : rowkeyList){ //把rowkey加到get里,再把get装到list中
  6. Get get = new Get(Bytes.toBytes(rowkey));
  7. getList.add(get);
  8. }
  9. Result[] results = table.get(getList); //重点在这,直接查getList<Get>
  10. for (Result result : results){ //对返回的结果集进行操作
  11. for (Cell kv : result.rawCells()) {
  12. String value = Bytes.toString(CellUtil.cloneValue(kv));
  13. list.add(value);
  14. }
  15. }
  16. return list;
  17. }

根据这种批量的方法,10000个rowkey进行测试,时间为2s,速度提升特别大。

ok!大功告成,成功解决了问题~

最近在生产中遇到了一个需求,前台给我多个rowkey的List,要在hbase中查询多个记录(返回给前台list)。在网上也查了很多,不过自己都不太满意,filter的功能有可能查询结果不是准确值,而网上给出的get方法也都是返回一条,scan的话都是返回全部数据,还有用rowkey范围查询的,都跟我的这个应用场景不符啊。无奈,自己找了一个方法,给各位有同样需求的朋友们一个参考。


首先创建链接属性:

  1. public static Configuration conf = null;
  2. public static Connection connection = null;
  3. public static Admin admin = null;
  4. static {
  5. conf = HBaseConfiguration.create();
  6. conf.set( "hbase.zookeeper.quorum", "10.120.193.800,10.120.193.810");
  7. conf.set( "hbase.master", "10.120.193.730:60010");
  8. conf.set( "hbase.zookeeper.property.clientPort", "2181");
  9. conf.set( "zookeeper.znode.parent", "/hbase-unsecure");
  10. conf.set( "hbase.client.keyvalue.maxsize", "1048576000"); //1G
  11. conf = HBaseConfiguration.create(conf);
  12. try {
  13. connection = ConnectionFactory.createConnection(conf);
  14. admin = connection.getAdmin();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }

紧接着,开始做查询操作:

  1. public static List<String> qurryTableTest(List<String> rowkeyList) throws IOException {
  2. String tableName = "table_a";
  3. Table table = connection.getTable( TableName.valueOf(tableName)); // 获取表
  4. for (String rowkey : rowkeyList){
  5. Get get = new Get(Bytes.toBytes(rowkey));
  6. Result result = table.get(get);
  7. for (Cell kv : result.rawCells()) {
  8. String value = Bytes.toString(CellUtil.cloneValue(kv));
  9. list.add(value);
  10. }
  11. }
  12. return list;
  13. }

但是!!!重点来了,每条rowkey都要发起一次请求,这种方法效率十分低,测试了10000条记录查询要用4分钟左右,项目需求是秒回啊,这怎么能行?

于是,就自己点开hbase java 源码,自己慢慢找,忽然眼前一亮!在HTable.class里看到了这个get方法:

  1. public Result[] get(List<Get> gets) throws IOException {
  2. if(gets.size() == 1) {
  3. return new Result[]{ this.get((Get)gets.get( 0))};
  4. } else {
  5. try {
  6. Object[] r1 = this.batch(gets);
  7. Result[] results = new Result[r1.length];
  8. int i = 0;
  9. Object[] arr$ = r1;
  10. int len$ = r1.length;
  11. for( int i$ = 0; i$ < len$; ++i$) {
  12. Object o = arr$[i$];
  13. results[i++] = (Result)o;
  14. }
  15. return results;
  16. } catch (InterruptedException var9) {
  17. throw (InterruptedIOException)( new InterruptedIOException()).initCause(var9);
  18. }
  19. }
  20. }

这就简单了,紧接着对自己上边的方法进行改进:

  1. public static List<String> qurryTableTestBatch(List<String> rowkeyList) throws IOException {
  2. List<Get> getList = new ArrayList();
  3. String tableName = "table_a";
  4. Table table = connection.getTable( TableName.valueOf(tableName)); // 获取表
  5. for (String rowkey : rowkeyList){ //把rowkey加到get里,再把get装到list中
  6. Get get = new Get(Bytes.toBytes(rowkey));
  7. getList.add(get);
  8. }
  9. Result[] results = table.get(getList); //重点在这,直接查getList<Get>
  10. for (Result result : results){ //对返回的结果集进行操作
  11. for (Cell kv : result.rawCells()) {
  12. String value = Bytes.toString(CellUtil.cloneValue(kv));
  13. list.add(value);
  14. }
  15. }
  16. return list;
  17. }

根据这种批量的方法,10000个rowkey进行测试,时间为2s,速度提升特别大。

ok!大功告成,成功解决了问题~

猜你喜欢

转载自blog.csdn.net/javastart/article/details/80940747