HBase学习之三: hbase filter(过滤器)和coprocessor(协处理器)统计行数的简单应用

  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服务器端执行,开发者可自定义处理器放置于服务器端然后再客户端调用,下面是一个简单的通过协处理器统计表行数的例子:

<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'
 现实情况是不可能统计一个表的行数前去命令行执行下命令,于是可在统计行数前加上如下代码:

<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>

猜你喜欢

转载自blog.csdn.net/javajxz008/article/details/51854611