hbase连接池

在web应用中,如果我们之间使用HTable来操作hbase, 那么在创建连接和关闭连接的时候,一定会浪费资源。那么HBase提供了一个连接池的技术。

主要涉及到的类和接口包括:HConnection, HConnectionManager, HTableInterface, ExecutorService(jdk提供的线程池)四个。

其中,HConnection就是hbase封装好的hbase连接池。

HConnectionManager是管理连接池的一个类。

HTableInterface是在类HTable的基础上进行的一个接口抽象。

ExecutorService是jdk的线程池对象。

static void testUseHbaseConnectionPool(Configuration conf) throws IOException {
    //创建两个线程进行操作
    ExecutorService threads = Executors.newFixedThreadPool(2);
    HConnection pool = HConnectionManager.createConnection(conf, threads);
    HTableInterface hTable = pool.getTable("users");
    try{
        testPut(hTable);
        testGet(hTable);
        testDelete(hTable);
        testScan(hTable);
    }finally {
        hTable.close(); // 每次htable操作完关闭,其实是放到pool中
        pool.close(); // 最终时候的关闭
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值