HTablePool为何弃用?
来源:互联网 发布:国产视觉软件 编辑:程序博客网 时间:2024/05/16 18:51
我们先看HConnection的getTable方法描述:
getTable
HTableInterface getTable(String tableName) throws IOException
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
HConnectionManager.createConnection(Configuration)
).- Parameters:
tableName
-- Returns:
- an HTable to use for interactions with this table
- Throws:
IOException
然后再联想到只要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(); log.info("Add a User:"+user.name+" successfully"); }
至于HConnection对象如何创建,HBase推荐使用的方法是:
createConnection
public static HConnection createConnection(org.apache.hadoop.conf.Configuration conf) throws IOException
Create a new HConnection instance using the passedconf
instance.Note: This bypasses the usual HConnection life cycle management done by
getConnection(Configuration)
. The caller is responsible for callingCloseable.close()
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:
conf
- configuration- Returns:
- HConnection object for
conf
- Throws:
ZooKeeperConnectionException
IOException
下面代码是我按照单例模式维护HConnection对象的例子:
public class HBaseUtils { private static final String QUORUM = "192.168.1.100"; private static final String CLIENTPORT = "2181"; private static Configuration conf = null; private static HConnection conn = null; /** * 获取全局唯一的Configuration实例 * @return */ public static synchronized Configuration getConfiguration() { if(conf == null) { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", QUORUM); conf.set("hbase.zookeeper.property.clientPort", CLIENTPORT); } return conf; } /** * 获取全局唯一的HConnection实例 * @return * @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()); } return conn; }}
- HTablePool为何弃用?
- Hbase连接池HTablePool为何弃用
- Hbase连接池HTablePool为何弃用
- HBase概念学习(九)HTablePool为何弃用?
- HBase概念学习(九)HTablePool为何弃用?
- AFNetworking3.0为何弃用了NSURLConnection
- Twitter:为何弃用MySQL转而使用NoSQL
- Google为何弃用HttpClient的而推荐使用HttpURLConnection
- 弃Maxthon用Firefox
- redhat弃用Xen
- .live()已经弃用!
- 果断弃用Eclipse
- iOS 弃用函数
- 此ID弃用
- 弃用 Blog
- cross_validation 模块弃用
- 弃。
- 北汽为何弃A股赴H股
- C++实现蛇形矩阵
- CodeForces 18A Triangle
- lua基础——基本语法
- 获取指定目录下的文件是否有以.docx结尾
- POJ 2531(状态压缩||DFS)
- HTablePool为何弃用?
- Linux 下java 安装
- 文件的判断
- 索引数组 和关联数组
- spring事务管理全面分析
- HDU 1272小希的迷宫
- Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置
- 判断文件是否有以.docx结尾
- 计数器的实现