java - Hbase连接池

标签 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.

关于java - Hbase连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40606393/

相关文章:

JAVA lo4j 正确的异常处理和日志记录

java - System.out.print() 中打印的常量的默认数据类型;

Hbase 和 HFile。它如何存储列族?

java - Hbase Java API TableNotDisabledException

Java 8 : getting default values for a collection with nested Optionals

java - 只有一种实现的接口(interface)

hadoop - 将数据从 HBase 迁移到文件系统。 (将 Reducer 输出写入本地或 Hadoop 文件系统)

hadoop - HDFS小文件设计

hadoop - 试图将超过 32 个 hfiles 加载到一个区域的一个家庭

java - 打印 java scriptlet 变量,就好像它是 JavaScript 变量一样