获取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();
            }

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值