hbase 1.2.4安装教程1.1.2的HTablePool已经被弃用,用什么来代替HTablePool

1314人阅读
hadoop(16)
可以解决HTable存在的线程不安全问题,同时通过维护固定数量的HTable对象,能够在程序运行期间复用这些HTable资源对象。
Configuration conf = HBaseConfiguration.create();
HTablePool pool = new HTablePool(conf, 10);
1.&& HTablePool可以自动创建HTable对象,而且对客户端来说使用上是完全透明的,可以避免多线程间数据并发修改问题。
2.&& HTablePool中的HTable对象之间是公用Configuration连接的,能够可以减少网络开销。
HTablePool的使用很简单:每次进行操作前,通过HTablePool的getTable方法取得一个HTable对象,然后进行put/get/scan/delete等操作,最后通过HTablePool的putTable方法将HTable对象放回到HTablePool中。
下面是个使用HTablePool的简单例子:
public void createUser(String username, String firstName, String lastName, String email, String password, String roles) throws IOException {
  HTable table = rm.getTable(UserTable.NAME);
  Put put = new Put(Bytes.toBytes(username));
  put.add(UserTable.DATA_FAMILY, UserTable.FIRSTNAME,
  Bytes.toBytes(firstName));
  put.add(UserTable.DATA_FAMILY, UserTable.LASTNAME,
    Bytes.toBytes(lastName));
  put.add(UserTable.DATA_FAMILY, UserTable.EMAIL, Bytes.toBytes(email));
  put.add(UserTable.DATA_FAMILY, UserTable.CREDENTIALS,
    Bytes.toBytes(password));
  put.add(UserTable.DATA_FAMILY, UserTable.ROLES, Bytes.toBytes(roles));
  table.put(put);
  table.flushCommits();
  rm.putTable(table);
至于多线程使用HTablePool的真实性能情况,需要通过实际的测试工作得到。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:754698次
积分:9160
积分:9160
排名:第1579名
原创:177篇
转载:81篇
评论:104条
(1)(3)(1)(1)(1)(2)(6)(1)(8)(4)(5)(1)(1)(5)(8)(9)(12)(21)(24)(4)(1)(4)(1)(9)(2)(12)(1)(1)(1)(1)(1)(2)(2)(1)(1)(2)(1)(1)(2)(2)(1)(1)(3)(3)(5)(2)(7)(6)(5)(3)(1)(3)(2)(10)(8)(20)(25)HBase概念学习(九)HTablePool为何弃用? - 推酷
HBase概念学习(九)HTablePool为何弃用?
我们先看HConnection的getTable方法描述:
getTable( tableName)
Retrieve an HTableInterface implementation for access to a table. The returned HTableInterface is not thread safe, a new instance should be created for each using thread. This is a lightweight operation, pooling or caching of the returned HTableInterface is neither required nor desired. Note that the HConnection needs to be unmanaged (created with
Parameters:
an HTable to use for interactions with this table
上面说HTable的的父类HTableInterface是非线程安全的,针对每个线程建议都应该创建一个新的HTableInterface实例,这个创建过程是轻量的,缓存HTableInterface对象既不是必须的也不是推荐的!
然后再联想到只要HTable使用的Configuration是同一个,那么它们一定是共用一个HConnection的,HConnection才是HBase客户端到Hbase集群的真正的连接。
再想想HTablePool的作用,无非就是HTable的连接池,里面维护的HTable应该来说都是使用的同一个HConnecion。
既然HTable的创建是轻量级的,使用同一个Confuguration的HTable都会共用一个HConnection,那么HTablePool就显得那么多余!
所以Hbase抛弃了HTablePool,我们唯一要做的就是保证HConnection实例是唯一的,全局共享的。然后针对HTableInterface对象最好在每次操作HBase表的时候根据HConnection对象来重新创建,使用完成之后及时关闭即可!
通过HConnection的getTable()方法就能够获取到用户操作HBase表的HTableInterface对象了。
下面是一个使用HConnection的getTable()方法获取HTableInterface对象的例子:
public void addUser(User user) throws IOException
HTableInterface usersTable = conn.getTable(TABLE_NAME);
Put put = makePut(user);
usersTable.put(put);
usersTable.close();
(&Add a User:&+user.name+& successfully&);
至于HConnection对象如何创建,HBase推荐使用的方法是:
createConnection
public static
createConnection(org.apache.hadoop.conf.Configuration conf)
Create a new HConnection instance using the passed
Note: This bypasses the usual HConnection life cycle management done by
. The caller is responsible for calling
on the returned connection instance. This is the recommended way to create HConnections.
HConnection connection = HConnectionManager.createConnection(conf); HTableInterface table = connection.getTable(&mytable&); table.get(...); ... table.close(); connection.close();
Parameters:
- configuration
HConnection object for
下面代码是我按照单例模式维护HConnection对象的例子:
public class HBaseUtils {
private static final String QUORUM = &192.168.1.100&;
private static final String CLIENTPORT = &2181&;
private static Configuration conf =
private static HConnection conn =
* 获取全局唯一的Configuration实例
public static synchronized Configuration getConfiguration()
if(conf == null)
HBaseConfiguration.create();
conf.set(&hbase.zookeeper.quorum&, QUORUM);
conf.set(&hbase.zookeeper.property.clientPort&, CLIENTPORT);
* 获取全局唯一的HConnection实例
* @throws ZooKeeperConnectionException
public static synchronized HConnection getHConnection() throws ZooKeeperConnectionException
if(conn == null)
* * 创建一个HConnection
* HConnection connection = HConnectionManager.createConnection(conf);
* HTableInterface table = connection.getTable(&mytable&);
* table.get(...); ...
* table.close();
* connection.close();
conn = HConnectionManager.createConnection(getConfiguration());
以上属于个人见解,如果有什么疑问和指教,欢迎指正。
可以联系邮箱: 交流,季义钦
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致HTablePool的替代者HConnection.getTable(String)
HBase社区从0.94版本开始,已经在筹划弃用HTablePool了,并且决定在0.99中正式弃用(可见HBASE-6580和HBASE-9117等)。在0.94.16 API中,已经可以看到:Deprecated. Use HConnection.getTable(String) instead.
HConnection.getTable的用法示例如下:
public class HConnectionTest {
private HConnection conn =
private Configuration conf =
* creating connection object for HBase, the pool size is 256 by default.
public QueryUtility() {
this.conf = HBaseConfiguration.create();
this.conf.set(&hbase.zookeeper.quorum&, &zk1,zk2,zk3&);
this.conf.set(&hbase.zookeeper.property.clientPort&, &2181&);
this.conn = HConnectionManager.createConnection(this.conf);
} catch (IOException e) {
System.out.println(&connection failed, process will exit&);
e.printStackTrace();
System.exit(1);
public void release()
this.conn.close();
} catch (IOException e) {
e.printStackTrace();
public void queryOneRecord(String strRowkey)
* the HTable object can also be set the class member.
HTable table = (HTable)this.conn.getTable(&testtable&);
Get get = new Get(strRowkey.getBytes());
Result rs = table.get(get);
String strRes = &this row & + strRowkey + & has & + rs.size()+ & versions&;
System.out.println(strRes);
table.close();
} catch (IOException e) {
e.printStackTrace();
实验发现:也可将HTable设置为类成员变量,这样在构造函数中添加语句:this.table=(HTable)this.conn.getTable(&testtable&),然后queryOneRecord方法中就无需再调用getTable方法了。当然,这样需要在release方法中调用this.table.close().
本分类共有文章7篇,更多信息详见
& 2012 - 2016 &
&All Rights Reserved. &
/*爱悠闲图+*/
var cpro_id = "u1888441";

我要回帖

更多关于 hbase1.2.4安装 的文章

 

随机推荐