【甘道夫】HBase连接池 -- HTablePool被Deprecated之后

说明:
最近两天在调研HBase的连接池,有了一些收获,特此记录下来。
本文先将官方文档(http://hbase.apache.org/book.html)9.3.1.1节翻译,方便大家阅读,然后查阅了关键类HConnectionManager的Developer API( http://hbase.apache.org/devapidocs/index.html) 做了一些总结。 
最后介绍一些阅读0.96、0.98及最新源码的精彩发现。

欢迎转载,请注明来源:
http://blog.csdn.net/u010967382/article/details/38046821

1.连接
HTable是HBase的client,负责从meta表中找到目标数据所在的RegionServers,当定位到目标RegionServers后,client直接和RegionServers交互,而不比再经过master。
HTable实例并不是线程安全的。 当需要创建HTable实例时,明智的做法是使用相同的HBaseConfiguration实例,这使得共享连接到RegionServers的ZK和socket实例,例如,应该使用这样的代码:
HBaseConfiguration conf = HBaseConfiguration.create();
HTable table1 = new HTable(conf, "myTable");
HTable table2 = new HTable(conf, "myTable");
而不是这样的代码:
HBaseConfiguration conf1 = HBaseConfiguration.create();
HTable table1 = new HTable(conf1, "myTable");
HBaseConfiguration conf2 = HBaseConfiguration.create();
HTable table2 = new HTable(conf2, "myTable");


2.连接池
当面对多线程访问需求时,我们可以预先建立HConnection,参见以下代码:

Example 9.1. Pre-Creating a HConnection

// Create a connection to the cluster.
HConnection connection = HConnectionManager.createConnection(Configuration);
HTableInterface table = connection.getTable("myTable");
// use table as needed, the table returned is lightweight
table.close();
// use the connection for other access to the cluster
connection.close();
构建HTableInterface实现是非常轻量级的,并且资源是可控的。

注意:
HTablePool是HBase连接池的老用法,该类在0.94,0.95和0.96中已经不建议使用,在0.98.1版本以后已经移除。

BTW:简陋的官方文档到此为止。。。。Orz

3.HConnectionManager
该类是连接池的关键,专门介绍。
HConnectionManager是一个不可实例化的类,专门用于创建HConnection。
最简单的创建HConnection实例的方式是 HConnectionManager.createConnection(config),该方法创建了一个连接到集群的HConnection实例,该实例被创建的程序管理。通过这个HConnection实例,可以使用 HConnection.getTable(byte[])方法取得 HTableInterface implementations的实现,例如 :
            HConnection connection = HConnectionManager . createConnection ( config );
            HTableInterface table = connection.getTable("tablename");
            try {
                // Use the table as needed, for a single operation and a single  thread
            } finally {
                table.close();
                connection.close();
            }  

3.1构造函数
无,不可实例化。

3.2常用方法
  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值