Hbase的连接池--HTablePool被Deprecated之后
1.连接
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实现是非常轻量级的,并且资源是可控的。
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();
}
package com.bigdata.dbhbase096; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes; public class ConnectionPoolTest {
private static final String QUORUM = "compute1";
private static final String CLIENTPORT = "2181";
private static final String TABLENAME = "minifiletable4";
private static Configuration conf = null;
private static HConnection conn = null; static{
try {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", QUORUM);
conf.set("hbase.zookeeper.property.clientPort", CLIENTPORT);
conn = HConnectionManager.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException {
HTableInterface htable = ConnectionPoolTest.conn.getTable(TABLENAME);
try {
Scan scan = new Scan();
ResultScanner rs = htable.getScanner(scan);
for (Result r : rs.next(5)) {
for (Cell cell : r.rawCells()) {
System.out.println("Rowkey : " + Bytes.toString(r.getRow())
+ " Familiy:Quilifier : "
+ Bytes.toString(CellUtil.cloneQualifier(cell))
+ " Value : "
+ Bytes.toString(CellUtil.cloneValue(cell))
+ " Time : " + cell.getTimestamp());
}
}
} finally {
htable.close();
} }
}
/**
* Get the connection that goes with the passed <code>conf</code> configuration instance.
* If no current connection exists, method creates a new connection and keys it using
* connection-specific properties from the passed {@link Configuration}; see
* {@link HConnectionKey}.
* @param conf configuration
* @return HConnection object for <code>conf</code>
* @throws ZooKeeperConnectionException
*/
@Deprecated
public static HConnection getConnection(final Configuration conf)
throws IOException {
HConnectionKey connectionKey = new HConnectionKey(conf);
synchronized (CONNECTION_INSTANCES) {
HConnectionImplementation connection = CONNECTION_INSTANCES.get(connectionKey);
if (connection == null) {
connection = (HConnectionImplementation)createConnection(conf, true);
CONNECTION_INSTANCES.put(connectionKey, connection);
} else if (connection.isClosed()) {
HConnectionManager.deleteConnection(connectionKey, true);
connection = (HConnectionImplementation)createConnection(conf, true);
CONNECTION_INSTANCES.put(connectionKey, connection);
}
connection.incCount();
return connection;
}
}
根据传入的conf构建HConnectionKey,然后以HConnectionKey实例为key到连接池Map对象CONNECTION_INSTANCES中去查找connection,如果找到就返回connection,如果找不到就新建,如果找到但已被关闭,就删除再新建。
我们来看HConnectionKey的构造函数:
HConnectionKey(Configuration conf) {
    Map<String, String> m = new HashMap<String, String>();
    if (conf != null) {
      for (String property : CONNECTION_PROPERTIES) {
        String value = conf.get(property);
        if (value != null) {
          m.put(property, value);
        }
      }
    }
    this.properties = Collections.unmodifiableMap(m);
    try {
      UserProvider provider = UserProvider.instantiate(conf);
      User currentUser = provider.getCurrent();
      if (currentUser != null) {
        username = currentUser.getName();
      }
    } catch (IOException ioe) {
      HConnectionManager.LOG.warn("Error obtaining current user, skipping username in HConnectionKey", ioe);
    }
  }  
由以上源码可知,接收conf构造HConnectionKey实例时,其实是将conf配置文件中的属性赋值给HConnectionKey自身的属性,换句话说,不管你new几次,只要conf的属性相同,new出来的HConnectionKey实例的属性都相同。
结论一:conf的属性 --》 HConnectionKey实例的属性
接下来,回到getConnection源码中看到这样一句话:
HConnectionImplementation connection = CONNECTION_INSTANCES.get(connectionKey);
该代码是以HConnectionKey实例为key来查找CONNECTION_INSTANCES这个LinkedHashMap中是否已经包含了HConnectionKey实例为key的键值对,这里要注意的是,map的get方法,其实获取的是key的hashcode,这个自己读JDK源码就能看到。
而HConnectionKey已经重载了hashcode方法:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
if (username != null) {
result = username.hashCode();
}
for (String property : CONNECTION_PROPERTIES) {
String value = properties.get(property);
if (value != null) {
result = prime * result + value.hashCode();
}
}
return result;
}
在该代码中,最终返回的hashcode取决于当前用户名及当前conf配置文件的属性。所以,只要conf配置文件的属性和用户相同,HConnectionKey实例的hashcode就相同!
结论二:conf的属性 --》HConnectionKey实例的hashcode
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();
/**
* Get the connection that goes with the passed <code>conf</code> configuration instance.
* If no current connection exists, method creates a new connection and keys it using
* connection-specific properties from the passed {@link Configuration}; see
* {@link HConnectionKey}.
* @param conf configuration
* @return HConnection object for <code>conf</code>
* @throws ZooKeeperConnectionException
*/
public static HConnection getConnection(final Configuration conf) throws IOException {
return ConnectionManager.getConnectionInternal(conf);
}
这个不是重点,重点是最新版本代码的pom:
  <groupId>org.apache.hbase</groupId>
40   <artifactId>hbase</artifactId>
   <packaging>pom</packaging>
 <version>2.0.0-SNAPSHOT</version>
  <name>HBase</name>
   <description>
     Apache HBase\99 is the Hadoop database. Use it when you need
     random, realtime read/write access to your Big Data.
    This project's goal is the hosting of very large tables -- billions of rows X millions of columns -- atop clusters
     of commodity hardware.
  </description>
Hbase的连接池--HTablePool被Deprecated之后的更多相关文章
- 【甘道夫】HBase连接池 -- HTablePool是Deprecated之后
		说明: 近期两天在调研HBase的连接池,有了一些收获,特此记录下来. 本文先将官方文档(http://hbase.apache.org/book.html)9.3.1.1节翻译,方便大家阅读,然后查 ... 
- 如何正确管理HBase的连接,从原理到实战
		本文将介绍HBase的客户端连接实现,并说明如何正确管理HBase的连接. 最近在搭建一个HBase的可视化管理平台,搭建完成后发现不管什么查询都很慢,甚至于使用api去listTable都要好几秒. ... 
- 连接池的实现 redis例子
		# -*- encoding:utf-8 -*- # import pymysql # # conn = pymysql.connect(host="127.0.0.1", por ... 
- mongoDB中的连接池(转载)
		一.mongoDB中的连接池 刚上手MongoDB,在做应用时,受以前使用关系型数据库的影响,会考虑数据库连接池的问题! 关系型数据库中,我们做连接池无非就是事先建立好N个连接(connection) ... 
- Spring Boot1.5.4 连接池 和 事务
		原文:https://github.com/x113773/testall/issues/10 默认连接池---spring Boot中默认支持的连接池有Tomcat.HikariCP .DBCP . ... 
- HttpClient4.X 升级 入门 + http连接池使用
		转载请注明出处,谢谢~ http://blog.csdn.net/shootyou/archive/2011/05/12/6415248.aspx 在一次服务器异常的排查过程当中(服务器异常排查的过程 ... 
- 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。  数据库连接不释放测试  连接池  释放连接 关闭连接  有关 redis-py 连接池会导致服务器产生大量 CLOSE_WAIT 的再讨论以及一个解决方案
		import pymysqlfrom redis import Redisimport time h, pt, u, p, db = '192.168.2.210', 3306, 'root', 'n ... 
- Http请求连接池 - HttpClient 的 PoolingHttpClientConnectionManager
		两个主机建立连接的过程是非常复杂的一个过程,涉及到多个数据包的交换,而且也非常耗时间.Http连接须要的三次握手开销非常大,这一开销对于比較小的http消息来说更大.但是假设我们直接使用已经建立好的h ... 
- 线程池-连接池-JDBC实例-JDBC连接池技术
		线程池和连接池 线程池的原理: 来看一下线程池究竟是怎么一回事?其实线程池的原理很简单,类似于操作系统中的缓冲区的概念,它的流程如下:先启动若干数量的线程,并让这些线程都处于睡眠状态,当客 ... 
随机推荐
- Windows Server 2003开机自动启动MySQL服务设置方法
			Windows Server 2003开机自动启动MySQL服务设置方法 发布时间:2014-12-19 更新时间:2014-12-24 来源:网络 作者:eaglezhong 关键词: 2003 e ... 
- VM虚拟机无法拖拽、粘贴、复制
			VM无法从客户机拖放/复制文件到虚拟机的解决办法: 将这两项取消勾选,点击[确定].再次打开,勾选,点击[确定] 原因分析:可能是VM中默认是不支持该功能的,但是在配置窗体上确实默认打钩打上的. 依据 ... 
- JavaWeb之 Servlet执行过程 与 生命周期
			Servlet的概念 什么是Servlet呢? Java中有一个叫Servlet的接口,如果一个普通的类实现了这个接口,这个类就是一个Servlet.Servlet下有一个实现类叫HttpServle ... 
- Activity(一)
			一个应用程序中至少包含一个Activity,Activity启动流程:当启动一个应用程序时,android操作系统会访问该应用程序的AndroidManifest.xml文件(该文件中说明了应用程序使 ... 
- java读取各类型的文件
			java读取各类型的文件 用到的几个包 bcmail-jdk14-132.jar/bcprov-jdk14-132.jar/checkstyle-all-4.2.jar/FontBox-0.1.0-d ... 
- Karaf 基于 osgi
			Karaf是Apache旗下的一个开源项目.Karaf同时也是一个基于OSGi的运行环境,Karaf提供了一个轻量级的OSGi容器,可以用于部署各种组件,应用程序.Karaf提供了很多特性用于帮助开发 ... 
- iOS学习之Object-C语言集合遍历和数组排序
			一.集合遍历 1.集合:OC中提供的容器类,数组,字典,集合. 2.遍历:对集合中元素依次取出的过程叫做遍历. 二.for循环遍历 1.通过for循环的循环变量用作数组元 ... 
- Swift Tips - Array 类型
			在开发中,数组这个概念我们应该很熟悉了,Objective-C 中为我们提供了 NSArray 作为数组的实现.大家应该对很熟悉了.而在 Swift 中,为我们提供了它自己对于数组的实现,也是这里我们 ... 
- 前端开发规范之html编码规范
			原则1.规范 .保证您的代码规范,趋html5,远xhtml,保证结构表现行为相互分离.2.简洁.保证代码的最简化,避免多余的空格.空行,保持代码的语义化,尽量使用具有语义的元素,避免使用样式属性和行 ... 
- java数据结构和算法------选择排序
			package iYou.neugle.sort; public class Select_sort { public static void SelectSort(double[] array) { ... 
