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 = null;
	private Configuration conf = null;

	/**
	 * 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");
		
		try {
			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()
	{
		try {
			this.conn.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void queryOneRecord(String strRowkey)
	{
		try {
			/**
			 * 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().

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值