Hbase1.1.2的htablepool 被弃用已经被弃用,用什么来代替htablepool 被弃用

Hbase1.1.2的HTablePool已经被弃用,用什么来代替HTablePool_百度知道Hbase利用HTablePool实现Htable连接池
之前通过直接new Htable方式对hbase表进行操作,会经常抛出NIOServerCnxn:
Too many connections from /10.202.50.79 - max is 60 异常解决
最后经过分析为Htable创建过多,而每创建一个htable就会创建一个连接,进行htable.close()进行关闭连接,虽然会执行操作,但不会及时清除,所以会出现zookeeper抛出连接过多的异常
有个很笨的方式就是在程序最初只创建一个Htable对象,这样就不会出现上述异常,
后来想到可以用关系数据库连接池方式实现htable连接池,正要实现发现了HTablePool类,就直接调用该类实现了。
返回一个Htable对象代码如下:
& & & & /**& & & &&
& & & &&&* 返回htablepool连接池中的一个htable
& & & &&&* @param tableName
& & & &&&* @return
& & & &&&*/
& & & & public static synchronized HTable getHtable(String tableName){
& & & & & & & & if(hTablePool!=null)
& & & & & & & & & & & & return (HTable) hTablePool.getTable(tableName);//如果hTablePool对象已经存在,直接取出一个htable
& & & & & & & & else{//& & & & hTablePool不存在则先new一个htablepool对象,然后再取& & & & & & & &&
& & & & & & & & & & & & String servicepath = HbaseService.class.getResource(&&).getPath()+&service.properties&;
& & & & & & & & & & & & Configuration configuration = HBaseConfiguration.create();
& & & & & & & & & & & & configuration.set(&hbase.master&,&&PropertyUtil.getValue(servicepath, &hbase.master&));
& & & & & & & & & & & & configuration.set(&hbase.zookeeper.quorum&, PropertyUtil.getValue(servicepath, &hbase.zookeeper.quorum&));
& & & & & & & & & & & & configuration.set(&hbase.zookeeper.property.clientPort&, PropertyUtil.getValue(servicepath, &hbase.zookeeper.property.clientPort&));
& & & & & & & & & & & & configuration.get(&hbase.master&);
& & & & & & & & & & & & hTablePool=new HTablePool(configuration,POOL_MAX_SIZE);//注意这个值设置的是每个htable表在pool中的最大值
& & & & & & & & & & & & return (HTable) hTablePool.getTable(tableName);
& & & & & & & & }
文章评论 以下网友留言只代表其个人观点,不代表本网站的观点和立场。6904人阅读
HBase(16)
源码分析(18)
这2周主要将项目中写hbase的模块中原来的异步hbaseclient改写成了使用hbase原生的HTable对象。大概总结下改写过程中和hj,xingchao发现的问题和解决方法。
1.HTablePool的基本使用方式:
由于HTable对象不是线程安全的,因此HBase提供HTablePool来支持多线程写入hbase,多线程同时从HTablePool中取出HTable并写入是安全的。HTablePool的使用方法类似数据库连接,使用时从HTablePool中取出一个HTable,使用完后再close放回HTablePool中。
Put put = new Put(rowkey);
put.add(LOG_COLUMN_FAMILY,HOST_NAME_QUALIFIER,values[0]);
HTableInterface table = HBaseClientFactory.getHTableByName(RAW_LOG_TABLE);
table.put(put);
} catch (IOException e) {
throw new RuntimeException(&Put Log meet exception&,e);
}finally { HBaseClientUtil.closeHTable(table);
2.HTablePool的maxsize。
HTablePool有一个maxsize,HTablePool针对每个表都有一个Pool,maxsize表示这个Pool的最大大小,在使用HTablePool的过程中我们发现这个值还是有需要注意的地方。
在多线程使用HTablePool拿到同一个表的HTable时,如果线程个数大于maxsize会导致写入始终是autoflush!
public HTableInterface getTable(String tableName) {
// call the old getTable implementation renamed to findOrCreateTable
HTableInterface table = findOrCreateTable(tableName);
// return a proxy table so when user closes the proxy, the actual table
// will be returned to the pool
return new PooledHTable(table);
当拿到HTable时会创建一个HTable对象并包装成一个PooledHTable对象。Pooled做了什么纳,其他方法都没变,只是在close时有所不同:
public void close() throws IOException {
returnTable(table);
}private void returnTable(HTableInterface table) throws IOException {
// this is the old putTable method renamed and made private
String tableName = Bytes.toString(table.getTableName());
if (tables.size(tableName) &= maxSize) {
// release table instance since we're not reusing it
this.tables.remove(tableName, table);
this.tableFactory.releaseHTableInterface(table);
tables.put(tableName, table);
可以看到如果tables.size大于maxsize,此时会去掉一个保存的HTable对象,而releaseHTableInterface实际调用的就是HTable的close方法,close方法又会强制flushHTable的buffer,因此,如果我们想不使用autoflush提升写入速度失效。
3.HTablePool type。
HTablePool提供了几种方式:ReusablePool,RoundRobinPool,ThreadLocalPool。默认的是reusable,由于2的原因,我们也可以考虑使用ThreadLocal的Pool,这样多线程写入时分别取自己线程的Pool,这样互不影响,写入的效率也会比较高。
static class ThreadLocalPool&R& extends ThreadLocal&R& implements Pool&R& {
private static final Map&ThreadLocalPool&?&, AtomicInteger& poolSizes = new HashMap&ThreadLocalPool&?&, AtomicInteger&();
public ThreadLocalPool() {
public R put(R resource) {
R previousResource = get();
if (previousResource == null) {
AtomicInteger poolSize = poolSizes.get(this);
if (poolSize == null) {
poolSizes.put(this, poolSize = new AtomicInteger(0));
poolSize.incrementAndGet();
this.set(resource);
return previousR
4.HTable的WriteBufferSize和autoflush
如果想追求写入的速度我们可以设置setWriteBufferSize为一个比较大的大小比如1M并autoflush为false,这样写入的速度会有几十倍的提升,但如果BufferSize比较大也会带来写入不够实时的问题,尤其有些表的数据很小会很久都不flush。因此,我们可以添加按时间间隔的flush方式。
public void put(final List&Put& puts) throws IOException {
super.put(puts);
needFlush();
private void needFlush() throws IOException {
long currentTime = System.currentTimeMillis();
if ((currentTime - lastFlushTime.longValue()) & flushInterval) {
super.flushCommits();
lastFlushTime.set(currentTime);
HTablePool可以设置自定义的HTableFactory来创建我们自定义的HTable。
pool = new HTablePool(conf, maxSize, tableFactory, PoolMap.PoolType.ThreadLocal);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:95299次
积分:1276
积分:1276
排名:千里之外
原创:30篇
评论:31条
(1)(1)(1)(1)(1)(2)(1)(3)(5)(4)(7)(3)Hbase1.1.2的HTablePool已经被弃用,用什么来代替HTablePool_百度知道}

我要回帖

更多关于 htablepool 过时 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信