我正在尝试创建 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.
关于java - Hbase连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40606393/