HBase学习笔记-统计表行数

来源:互联网 发布:seo发展方向 编辑:程序博客网 时间:2024/06/05 14:58

HBase统计表行数的方式如下:

一、HBase自带MapReduce表行数统计RowCounter

$HBASE_HOME/bin/hbase org.apache.Hadoop.hbase.mapreduce.RowCounter ‘tableName’


二、使用Scan与Filter的方式对表行数进行统计

[java] view plain copy
  1. public static long rowCount(String tableName) {  
  2.     long rowCount = 0;  
  3.     try {  
  4.         HTable table = new HTable(configuration, tableName);  
  5.         Scan scan = new Scan();  
  6.         scan.setFilter(new FirstKeyOnlyFilter());  
  7.         ResultScanner resultScanner = table.getScanner(scan);  
  8.         for (Result result : resultScanner) {  
  9.             rowCount += result.size();  
  10.         }  
  11.     } catch (IOException e) {  
  12.         logger.info(e.getMessage(), e);  
  13.     }  
  14.     return rowCount;  
  15. }  


三、使用Coprocessor新特性来对表行数进行统计

[java] view plain copy
  1. public static void addTableCoprocessor(String tableName, String coprocessorClassName) {  
  2.     try {  
  3.         admin.disableTable(tableName);  
  4.         HTableDescriptor htd = admin.getTableDescriptor(Bytes.toBytes(tableName));  
  5.         htd.addCoprocessor(coprocessorClassName);  
  6.         admin.modifyTable(Bytes.toBytes(tableName), htd);  
  7.         admin.enableTable(tableName);  
  8.     } catch (IOException e) {  
  9.         logger.info(e.getMessage(), e);  
  10.     }  
  11. }  
  12.       
  13. public static long rowCount(String tableName, String family) {  
  14.     AggregationClient ac = new AggregationClient(configuration);    
  15.     Scan scan = new Scan();  
  16.     scan.addFamily(Bytes.toBytes(family));  
  17.     long rowCount = 0;  
  18.     try {  
  19.         rowCount = ac.rowCount(Bytes.toBytes(tableName), new LongColumnInterpreter(), scan);  
  20.     } catch (Throwable e) {  
  21.         logger.info(e.getMessage(), e);  
  22.     }    
  23.     return rowCount;  
  24. }  
[java] view plain copy
  1. @Test  
  2. public void testTableRowCount() {  
  3.     String coprocessorClassName = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation";  
  4.     HBaseUtils.addTableCoprocessor("user", coprocessorClassName);  
  5.     long rowCount = HBaseUtils.rowCount("user""basic");  
  6.     System.out.println("rowCount: " + rowCount);  
  7. }  
0 0
原创粉丝点击