分享

HTablePool简单使用例子

 IT技术武馆 2014-08-01

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 hts = new ArrayList();
  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;
  
  // System.out.println("result " + i + result.toString());
  }

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

 }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多