写了一个用HTablePool来做HTable连接池的代码,不对的地方请多指教
直接上代码,仔细看看,应该不难看懂
package com.pzoom.hbase.java;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
public class TestHTablePool {
private static Configuration conf ;
static{
conf = new Configuration() ;
conf.set("hbase.zookeeper.quorum", "192.168.1.171,192.168.1.172,192.168.1.173");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
@SuppressWarnings("deprecation")
public void testTablePool() throws IOException {
HTablePool tablePool = new HTablePool(conf, 10);
ArrayList<HTableInterface> hts = new ArrayList<HTableInterface>();
try {
for(int i=0; i <6; i++){
HTableInterface hTable = tablePool.getTable(Bytes.toBytes("tab_keyword"));
System.out.println("the number:" + i);
hts.add(hTable);
}
} catch (Exception e) {
System.out.println("error!");// TODO: handle exception
e.printStackTrace();
}
System.out.println("tablepool size: " + hts.size());
Scan scan = new Scan();
// PageFilter filter = new PageFilter(1);
// scan.setFilter(filter);
scan.setStartRow(Bytes.toBytes("2012-08-03:242969862:0000036"));
scan.setStopRow(Bytes.toBytes("2012-08-03:242969862:0000037"));
ResultScanner rs = hts.get(0).getScanner(scan);
System.out.println("begin!");
for (Result result : rs) {
System.out.println(Bytes.toString(result.getRow()));
for (KeyValue kv : result.raw()) {
// if("text".equals(Bytes.toString(kv.getQualifier()))) {
// System.out.println("q: " + Bytes.toString(kv.getQualifier()) + " :"
// + Bytes.toString(kv.getValue()));
// }
}
}
rs.close();
for (HTableInterface hTable : hts) {
tablePool.putTable(hTable);
}
}
@Test
public void testPool() throws IOException {
testTablePool();
}
}