Hbase连接池

use*_*464 5 java hbase

我正在尝试创建 hbase 连接池。我已经尝试过以下事情。但我不知道后果。它会影响我的表现吗?有人可以帮忙吗?主机可以是远程的,甚至可以是本地的。

HashMap cons = new HashMap();
    public void getDataFromHbase(String host, String tableid){
        conf.set("hbase.zookeeper.quorum", host);
        ThreadPoolExecutor executor= (ThreadPoolExecutor) Executors.newCachedThreadPool();
        executor.setMaximumPoolSize(50);
        if(cons.get(host+"tableA_"+tableid) != null){
            table1 = cons.get(host+"tableA_"+tableid);
            table2 = cons.get(host+"tableB_"+tableid);
        }
        else{
            table1 = new HTable(conf,Bytes.toBytes("tableA_"+tableid),executor);
            table2 = new HTable(conf,Bytes.toBytes("tableB_"+tableid),executor);
            cons.put(host+"tableA_"+tableid,table1);
            cons.put(host+"tableB_"+tableid,table2);
        }
        Scan scan = new Scan();
        scan.addFamily(Bytes.toBytes("n"));
        scan.setCaching(1000);
        ResultScanner resultScanner = table1.getScanner(scan);
        ResultScanner resultScannerB = table2.getScanner(scan);
    }
Run Code Online (Sandbox Code Playgroud)

Ram*_*ram 1

我推荐HTablePool .. 而不是自己的连接管理,它更容易出错并且难以调试

类 HTablePool

java.lang.Object org.apache.hadoop.hbase.client.HTablePool

所有实现的接口:

可关闭、可自动关闭

已弃用。请改用 HConnection.getTable(String)。

公共类 HTablePool 扩展 Object 实现 Closeable

一个简单的 HTable 实例池。每个 HTablePool 充当所有表的池。要使用,请实例化 HTablePool 并使用 getTable(String) 从池中获取 HTable。不再需要此方法,客户端应该调用 HTableInterface.close() 而不是将表返回到池中。完成后,通过调用 HTableInterface.close() 关闭 HTableInterface 实例,而不是将表返回到池中使用(已弃用)putTable(HTableInterface)。可以使用 maxSize 创建池,它定义将为每个表保留的最多 HTable 引用。否则,默认值为 Integer.MAX_VALUE。

池将管理自己与集群的连接。请参阅 HConnectionManager