HTablePool简单使用例子

10 篇文章 0 订阅
7 篇文章 0 订阅
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)

 

[java]  view plain copy
  1. public static void testTablePool() throws IOException {  
  2.     HTablePool tablePool = new HTablePool(conf, 5);  
  3.     ArrayList<HTableInterface> hts = new ArrayList<HTableInterface>();  
  4.     try {  
  5.     for(int i=0; i <6; i++){  
  6.         HTableInterface hTable =  tablePool.getTable(Bytes.toBytes("testtable"));  
  7.         System.out.println("the number:" + i);  
  8.         hts.add(hTable);  
  9.     }  
  10.     } catch (Exception e) {  
  11.         System.out.println("error!");// TODO: handle exception  
  12.         e.printStackTrace();  
  13.     }  
  14.     System.out.println("tablepool size: " + hts.size());  
  15.     Scan scan = new Scan();  
  16.     int number = 0;  
  17.     PageFilter filter = new PageFilter(1);  
  18.     scan.setFilter(filter);  
  19.     ResultScanner rs = hts.get(0).getScanner(scan);  
  20.     System.out.println("begin!");  
  21.     int i = 0;  
  22.   
  23.     for (Result result : rs) {  
  24.         System.out.println("number:" + (++i));  
  25.         number = 0;  
  26.     /*  for (KeyValue kv : result.list()) { 
  27.             number++; 
  28.             System.out.println("number: " + number + "  :" 
  29.                     + Bytes.toString(kv.getKey())); 
  30.             System.out.println(Bytes.toString(kv.getValue())); 
  31.         }*/  
  32.     //  System.out.println("result " + i + result.toString());  
  33.     }  
  34.   
  35.     rs.close();  
  36.     for (HTableInterface hTable : hts) {  
  37.         tablePool.putTable(hTable);  
  38.     }  
  39.       
  40.   
  41. }  


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值