现在的位置: 首页 > 综合 > 正文

org.apache.hadoop.hbase.coprocessor.AggregateImplementation 来统计hbase表的行数

2016年07月04日 ⁄ 综合 ⁄ 共 1548字 ⁄ 字号 评论关闭

hbase自带了一个聚合coprocessor类:org.apache.hadoop.hbase.coprocessor.AggregateImplementation。使用该类可以count一张表的总记录数。

当然在hbase shell下面也可以count <table_name>来统计。我这里比较了一下两者的执行时间,我有一张表有700多万的数据,在hbase shell下count足足花费了我12分钟的时间,而用coprocessor来统计,只花费了78秒!!!由此可见coprocessor的强大。

hbase aip 添加协处理器:

[java] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. Configuration hbaseconfig = HBaseConfiguration.create();  
  2.   
  3. HBaseAdmin hbaseAdmin = new HBaseAdmin(hbaseconfig);  
  4. hbaseAdmin.disableTable(TABLE_NAME);  
  5.   
  6. HTableDescriptor htd = hbaseAdmin.getTableDescriptor(TABLE_NAME);  
  7. htd.addCoprocessor(AggregateImplementation.class.getName());  
  8. hbaseAdmin.modifyTable(TABLE_NAME, htd);  
  9. hbaseAdmin.enableTable(TABLE_NAME);  
  10. hbaseAdmin.close();  


使用hbase提供的聚合coprocessor:

[java] view
plain
copy在CODE上查看代码片派生到我的代码片

  1. AggregationClient aggregationClient = new AggregationClient(hbaseconfig);  
  2.                Scan scan = new Scan();  
  3.                scan.addFamily(Bytes.toBytes("fr"));  
  4.                Date start = new Date();  
  5.                long rowcount = aggregationClient.rowCount(TABLE_NAME,  
  6.                                new LongColumnInterpreter(), scan);  
  7.                Date end = new Date();  
  8.                System.out.println("rowcount:" + rowcount);  
  9.                System.out.println("timecost:" + (end.getTime() - start.getTime()));  


hbase shell添加coprocessor:

disable 'member'
alter 'member',METHOD => 'table_att','coprocessor' => 'hdfs://master24:9000/user/hadoop/jars/test.jar|mycoprocessor.SampleCoprocessor|1001|'
enable 'member'

hbase shell 删除coprocessor:

disable 'member'
alter 'member',METHOD => 'table_att_unset',NAME =>'coprocessor$1'
enable 'member'

抱歉!评论已关闭.