hbase2.0利用协处理器(coprocessor)统计行数

一、启动协处理器

在hbase集群环境中,在hbase-site.xml配置文件中添加属性信息如下:


   hbase.coprocessor.user.region.classes
   org.apache.hadoop.hbase.coprocessor.AggregateImplementation
 

二、实现代码如下:

1)初始化连接

//初始化连接
    public static void init() throws IOException {
        config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum","192.168.21.28");
        config.set("hbase.zookeeper.property.clientPort", "2181");
        config.set("hbase.master", "192.168.21.28:60010");
        //使用配置创建一个连接
        conn = ConnectionFactory.createConnection(config);
        admin = (HBaseAdmin) conn.getAdmin();//得到管理员
    }

2)统计行数

public  static long  rowCount(String tablename){
        long rowCount = 0;
        try {
            init();
            TableName name=TableName.valueOf(tablename);
            //先disable表,添加协处理器后再enable表
            admin.disableTable(name);
            TableDescriptor tableDescriptor = admin.getDescriptor(name);
            String coprocessorClass = "org.apache.hadoop.hbase.client.coprocessor.AggregationClient";
            if (! tableDescriptor.hasCoprocessor(coprocessorClass)) {
                tableDescriptor.getCoprocessorDescriptors();
            }
            admin.modifyTable(name, tableDescriptor);
            admin.enableTable(name);
            //计时
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            Scan scan = new Scan();
            AggregationClient aggregationClient = new AggregationClient(config);
            rowCount = aggregationClient.rowCount(name, new LongColumnInterpreter(), scan);
            System.out.println("RowCount: " + rowCount);
            stopWatch.stop();
            System.out.println("统计耗时:" +stopWatch.now(TimeUnit.SECONDS) +"s");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return rowCount;
    }

三、对比hbase shell统计方式

1)shell方式:

2)协处理器方式:

你可能感兴趣的:(hbase)