1.Java代码实现
importorg.apache.hadoop.hbase.client.coprocessor.AggregationClient;importorg.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;importorg.apache.hadoop.hbase.coprocessor.AggregateImplementation;/***
* 协处理器统计HBase表数据量
*
**/
public classHBaseRecordsCounter {/*** HBase API添加协处理器
**/
public static voidaddCoprocessor(Configuration conf, String tableName) {try{
byte[] tableNameBytes = Bytes.toBytes(tableName);
HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
HTableDescriptor htd = hbaseAdmin.getTableDescriptor(tableNameBytes);
if (!htd.hasCoprocessor(AggregateImplementation.class.getName())) {
hbaseAdmin.disableTable(tableNameBytes);
htd.addCoprocessor(AggregateImplementation.class.getName());
hbaseAdmin.modifyTable(tableNameBytes, htd);
hbaseAdmin.enableTable(tableNameBytes);
}
hbaseAdmin.close();
}catch(MasterNotRunningException e) { e.printStackTrace(); }catch(ZooKeeperConnectionException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } }/*** 统计表数量 **/
public static voidexeCount(Configuration conf, String tableName, String family) {try{//使用hbase提供的聚合coprocessor
AggregationClient aggregationClient = newAggregationClient(conf); Scan scan= newScan();//指定扫描列族,唯一值
scan.addFamily(Bytes.toBytes(family));long start =System.currentTimeMillis();long rowCount = aggregationClient.rowCount(TableName.valueOf(tableName), newLongColumnInterpreter(), scan); System.out .println("Row count: " + rowCount + "; time cost: " + (System.currentTimeMillis() - start) + "ms"); }catch(Throwable e) { e.printStackTrace(); } }public static voidmain(String[] args) { String tableName= "test"; Configuration conf= newConfiguration(); conf.set("hbase.zookeeper.quorum", "host1,host2,host3"); conf.set("hbase.rootdir", "hdfs://host:8020/hbase");//提高RPC通信时长
conf.setLong("hbase.rpc.timeout", 600000);//设置Scan缓存
conf.setLong("hbase.client.scanner.caching", 1000);addCoprocessor(conf, tableName);exeCount(conf, tableName,"info"); } }
2. 启用协处理器
启用协处理器方法1.
启动全局aggregation,能过操纵所有的表上的数据。通过修改hbase-site.xml这个文件来实现,只需要添加如下代码:
hbase.coprocessor.user.region.classes
org.apache.hadoop.hbase.coprocessor.AggregateImplementation
启用协处理器方法2.
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’