hbase filter(过滤器)和coprocessor(协处理器)统计行数的简单应用

来源:互联网 发布:夏朝存在吗 知乎 编辑:程序博客网 时间:2024/05/21 14:07

  1.关于filter的用法及说明参见这三篇博文,基本涵盖了绝大部分filter,很详细,还有实测代码,感谢博主的付出,特收藏.
HBase Filter:
http://blog.sina.com.cn/s/blog_7431c7c50101c5ig.html
HBase(0.96以上版本)过滤器Filter详解及实例代码:http://blog.csdn.net/u010967382/article/details/37653177
HBase Filter介绍及执行流程:http://my.oschina.net/cloudcoder/blog/289649

2.通过coprocessor统计行数

  关于协处理器的介绍和用法网上很多资料,可以自行查找脑补,值得注意的是协处理器在hbase服务器端执行,开发者可自定义处理器放置于服务器端然后再客户端调用,下面是一个简单的通过协处理器统计表行数的例子:

[java] view plain copy
print?
  1. <span style=“font-family:Arial Black;”><span style=“font-family:Times New Roman;”>  AggregationClient  aggregation = new AggregationClient(conf);  
  2.   Long count = aggregation.rowCount(table, new LongColumnInterpreter(), s);//table为HTable实例,s为Scan实例  
  3.   int totalCount = count.intValue();</span></span>  
<span style="font-family:Arial Black;"><span style="font-family:Times New Roman;">  AggregationClient  aggregation = new AggregationClient(conf);  Long count = aggregation.rowCount(table, new LongColumnInterpreter(), s);//table为HTable实例,s为Scan实例  int totalCount = count.intValue();</span></span>
要使得上述代码生效,还必须需要让要统计的表具有聚合功能。如下在hbase shell执行下面的命令:

 * disable ‘emp’
 * alter ‘emp’,METHOD=>’table_att’,’coprocessor’=>’|org.apache.hadoop.hbase.coprocessor.AggregateImplementation||’
 * enable ‘emp’
要删除协处理器,如下:
 * disable ‘emp’
 * alter ‘emp’,METHOD=>’table_att_unset’,NAME=>’coprocessor$1’
 * enable ‘emp’
 现实情况是不可能统计一个表的行数前去命令行执行下命令,于是可在统计行数前加上如下代码:

[java] view plain copy
print?
  1. <span style=“font-family:Arial Black;”><span style=“font-family:Times New Roman;”>  /** 
  2.      * 使表具有聚合功能 
  3.  *  
  4.      * @param tableName 
  5.      *            表名 
  6.  */  
  7.     @SuppressWarnings(“resource”)  
  8.     private void enableAggregation(String tableName) {  
  9.     String coprocessorName = ”org.apache.hadoop.hbase.coprocessor.AggregateImplementation”;  
  10.     try {  
  11.     HBaseAdmin admin = new HBaseAdmin(conf);  
  12.     HTableDescriptor htd = admin.getTableDescriptor(Bytes  
  13.     .toBytes(tableName));  
  14.     List<String> coprocessors = htd.getCoprocessors();  
  15.     if (coprocessors != null && coprocessors.size() > 0) {  
  16.     return;  
  17.     } else {  
  18.     admin.disableTable(tableName);  
  19.     htd.addCoprocessor(coprocessorName);  
  20.     admin.modifyTable(tableName, htd);  
  21.     admin.enableTable(tableName);  
  22.     }  
  23.     } catch (TableNotFoundException e) {  
  24.     // TODO Auto-generated catch block  
  25.     log.error(e);  
  26.     } catch (MasterNotRunningException e) {  
  27.     // TODO Auto-generated catch block  
  28.     log.error(e);  
  29.     } catch (ZooKeeperConnectionException e) {  
  30.     // TODO Auto-generated catch block  
  31.     log.error(e);  
  32.     } catch (IOException e) {  
  33.     // TODO Auto-generated catch block  
  34.     log.error(e);  
  35.     }  
  36.     }</span></span>  
<span style="font-family:Arial Black;"><span style="font-family:Times New Roman;">   /**     * 使表具有聚合功能 *      * @param tableName     *            表名 */    @SuppressWarnings("resource")    private void enableAggregation(String tableName) {    String coprocessorName = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation";    try {    HBaseAdmin admin = new HBaseAdmin(conf);    HTableDescriptor htd = admin.getTableDescriptor(Bytes    .toBytes(tableName));    List<String> coprocessors = htd.getCoprocessors();    if (coprocessors != null && coprocessors.size() > 0) {    return;    } else {    admin.disableTable(tableName);    htd.addCoprocessor(coprocessorName);    admin.modifyTable(tableName, htd);    admin.enableTable(tableName);    }    } catch (TableNotFoundException e) {    // TODO Auto-generated catch block    log.error(e);    } catch (MasterNotRunningException e) {    // TODO Auto-generated catch block    log.error(e);    } catch (ZooKeeperConnectionException e) {    // TODO Auto-generated catch block    log.error(e);    } catch (IOException e) {    // TODO Auto-generated catch block    log.error(e);    }    }</span></span>

阅读全文
0 0
原创粉丝点击