获取hbase中表的记录数

获取hbase中表的记录数


    /**
     * 获取hbase中表的记录数 , 此方法查询记录数较快 可到秒级
     *
     * @param zkIp
     * @param zkPort
     * @param hbaseZNode
     * @param name
     * @return
     */
    public static long getTableRowNum(String zkIp,
                                      String zkPort,
                                      String hbaseZNode,
                                      String name) throws Throwable {

        long RowCount = 0;
        Configuration configuration = HBaseConfiguration.create();

        configuration.set("hbase.zookeeper.quorum", zkIp);// zookeeper地址
        configuration.set("hbase.zookeeper.property.clientPort", zkPort);// zookeeper端口2181

        //获取超时时间配置
        int hbase_client_operation_timeout = Integer.parseInt(CONFIG.getConfig("hbase.client.operation.timeout", "30000"));
        int hbase_rpc_timeout = Integer.parseInt(CONFIG.getConfig("hbase.rpc.timeout", "60000"));
        int hbase_client_pause = Integer.parseInt(CONFIG.getConfig("hbase.client.pause", "500"));
        int hbase_client_retries_number = Integer.parseInt(CONFIG.getConfig("hbase.client.retries.number", "3"));

        configuration.set("zookeeper.znode.parent", hbaseZNode);
        configuration.setInt("hbase.rpc.timeout", hbase_rpc_timeout);
        configuration.setInt("hbase.client.operation.timeout", hbase_client_operation_timeout);
        //重试等待时间
        configuration.setInt("hbase.client.pause", hbase_client_pause);
        //默认重试次数
        configuration.setInt("hbase.client.retries.number", hbase_client_retries_number);


        Connection connection = null ;
        AggregationClient aggregationClient = null ;
        try {
            connection = ConnectionFactory.createConnection(configuration);
            Admin admin = connection.getAdmin();
            TableName tableName = TableName.valueOf(name);

            //先disable表,添加协处理器后再enable表
            admin.disableTable(tableName);
            HTableDescriptor descriptor = admin.getTableDescriptor(tableName);
            String coprocessorClass = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation";
            if (!descriptor.hasCoprocessor(coprocessorClass)) {
                descriptor.addCoprocessor(coprocessorClass);
            }
            admin.modifyTable(tableName, descriptor);
            admin.enableTable(tableName);

            Scan scan = new Scan();
            aggregationClient = new AggregationClient(configuration);
            RowCount = aggregationClient.rowCount(tableName, new LongColumnInterpreter(), scan);

            return RowCount;
        }catch (Exception e){
            log.error("获取hbase记录数:"+e.getMessage());
            throw e;
        }finally {
            log.info("关闭hbase连接");
            if(connection != null){
                connection.close();
            }
            if(aggregationClient != null){
                aggregationClient.close();
            }

        }

    }

你可能感兴趣的:(Hbase,JAVA)