获取hbase中表的记录数
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);
configuration.set("hbase.zookeeper.property.clientPort", zkPort);
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);
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();
}
}
}