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().