Hbase quickly count number of rows

前端 未结 12 1566
轮回少年
轮回少年 2020-12-04 13:25

Right now I implement row count over ResultScanner like this

for (Result rs = scanner.next(); rs != null; rs = scanner.next()) {
    number++;
}         


        
12条回答
  •  攒了一身酷
    2020-12-04 13:50

    U can find sample example here:

    /**
         * Used to get the number of rows of the table
         * @param tableName
         * @param familyNames
         * @return the number of rows
         * @throws IOException
         */
        public long countRows(String tableName, String... familyNames) throws IOException {
            long rowCount = 0;
            Configuration configuration = connection.getConfiguration();
            // Increase RPC timeout, in case of a slow computation
            configuration.setLong("hbase.rpc.timeout", 600000);
            // Default is 1, set to a higher value for faster scanner.next(..)
            configuration.setLong("hbase.client.scanner.caching", 1000);
    
            AggregationClient aggregationClient = new AggregationClient(configuration);
            try {
                Scan scan = new Scan();
                if (familyNames != null && familyNames.length > 0) {
                    for (String familyName : familyNames) {
                        scan.addFamily(Bytes.toBytes(familyName));
                    }
                }
                rowCount = aggregationClient.rowCount(TableName.valueOf(tableName), new LongColumnInterpreter(), scan);
            } catch (Throwable e) {
                throw new IOException(e);
            }
            return rowCount;
        }
    

提交回复
热议问题