HTablePool简单使用例子

HTablePool用在经常要创建表关闭表的程序中,可以大大优化性能,提高效率。

HTablePool()
HTablePool(Configuration config, int maxSize)
HTablePool(Configuration config, int maxSize, HTableInterfaceFactory tableFactory)
maxSize并非tablepool可创建的最大数,可以创建远大于设定值的表对象,当putTable()即返回时会与maxSize比较当前空闲数,空闲的已经有maxSize个,则直接释放表,不放入tablePool里
HTableInterface getTable(String tableName)
HTableInterface getTable(byte[] tableName)

void putTable(HTableInterface table)

 

	public static void testTablePool() throws IOException {
		HTablePool tablePool = new HTablePool(conf, 5);
		ArrayList<HTableInterface> hts = new ArrayList<HTableInterface>();
		try {
		for(int i=0; i <6; i++){
			HTableInterface hTable =  tablePool.getTable(Bytes.toBytes("testtable"));
			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();
		int number = 0;
		PageFilter filter = new PageFilter(1);
		scan.setFilter(filter);
		ResultScanner rs = hts.get(0).getScanner(scan);
		System.out.println("begin!");
		int i = 0;

		for (Result result : rs) {
			System.out.println("number:" + (++i));
			number = 0;
		/*	for (KeyValue kv : result.list()) {
				number++;
				System.out.println("number: " + number + "  :"
						+ Bytes.toString(kv.getKey()));
				System.out.println(Bytes.toString(kv.getValue()));
			}*/
		//	System.out.println("result " + i + result.toString());
		}

		rs.close();
		for (HTableInterface hTable : hts) {
			tablePool.putTable(hTable);
		}
		

	}


conf 与正常建htable时的配置相同即可。

你可能感兴趣的:(HTablePool简单使用例子)