微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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);
    }

解决方法:

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

Class HTablePool

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

All Implemented Interfaces:

Closeable, AutoCloseable

Deprecated. Use HConnection.getTable(String) instead.

public class HTablePool extends Object implements Closeable

A simple pool of HTable instances. Each HTablePool acts as a pool for
all tables. To use, instantiate an HTablePool and use getTable(String)
to get an HTable from the pool. This method is not needed anymore,
clients should call HTableInterface.close() rather than returning the
tables to the pool Once you are done with it, close your instance of
HTableInterface by calling HTableInterface.close() rather than
returning the tables to the pool with (deprecated)
putTable(HTableInterface). A pool can be created with a maxSize which
defines the most HTable references that will ever be retained for each
table. Otherwise the default is Integer.MAX_VALUE.

Pool will manage its own connections to the cluster. See
HConnectionManager.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐