HBase统计表数据的方法

转自:https://blog.csdn.net/Abysscarry/article/details/82861425

package com.zjxt.demo.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.util.StopWatch;

import java.io.IOException;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;

public class HBase {

    private static Connection connection =null;
    private static Admin admin =null;
    private static Configuration conf =null;

    static {

        System.setProperty("HADOOP_USER_NAME", "hbase");

        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "xxxx-n1,xxxx-n2,xxxx-n3");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("zookeeper.znode.parent", "/hbase-unsecure");

        try {
            connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void rowCountByCoprocessor(String tablename){
        try {
            //提前创建connection和conf
            Admin admin = connection.getAdmin();
            TableName name=TableName.valueOf(tablename);
            //先disable表,添加协处理器后再enable表
            admin.disableTable(name);
            HTableDescriptor descriptor = admin.getTableDescriptor(name);
            String coprocessorClass = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation";
            if (! descriptor.hasCoprocessor(coprocessorClass)) {
                descriptor.addCoprocessor(coprocessorClass);
            }
            admin.modifyTable(name, descriptor);
            admin.enableTable(name);

            //计时
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();

            Scan scan = new Scan();
            AggregationClient aggregationClient = new AggregationClient(conf);

            System.out.println("RowCount: " + aggregationClient.rowCount(name, new LongColumnInterpreter(), scan));
            stopWatch.stop();
            System.out.println("统计耗时:" +stopWatch.now(TimeUnit.SECONDS));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException, ParseException {
        rowCountByCoprocessor("TEST1");

    }


}

 

你可能感兴趣的:(小技巧,hbase统计表数据)