HTablePool实例代码

写了一个用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();
	}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值