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.  
     
  7.  
    conf.set( "hbase.zookeeper.quorum", "10.120.193.800,10.120.193.810");
  8.  
    conf.set( "hbase.master", "10.120.193.730:60010");
  9.  
    conf.set( "hbase.zookeeper.property.clientPort", "2181");
  10.  
    conf.set( "zookeeper.znode.parent", "/hbase-unsecure");
  11.  
    conf.set( "hbase.client.keyvalue.maxsize", "1048576000"); //1G
  12.  
    conf = HBaseConfiguration.create(conf);
  13.  
    try {
  14.  
    connection = ConnectionFactory.createConnection(conf);
  15.  
    admin = connection.getAdmin();
  16.  
    catch (IOException e) {
  17.  
    e.printStackTrace();
  18.  
    }
  19.  
    }


緊接着,開始做查詢操作:

 

  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.  
     
  12.  
    for(int i$ = 0; i$ < len$; ++i$) {
  13.  
    Object o = arr$[i$];
  14.  
    results[i++] = (Result)o;
  15.  
    }
  16.  
     
  17.  
    return results;
  18.  
    catch (InterruptedException var9) {
  19.  
    throw (InterruptedIOException)(new InterruptedIOException()).initCause(var9);
  20.  
    }
  21.  
    }
  22.  
    }


這就簡單了,緊接着對自己上邊的方法進行改進:

 

  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!大功告成,成功解決了問題~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM