HBase笔记--自定义filter
自定义filter需要继承的类:FilterBase
类里面的方法调用顺序
| 方法名 | 作用 | |
| 1 | boolean filterRowKey(Cell cell) | 根据row key过滤row。如果需要被过滤掉,返回true;需要返回给客户端,返回false |
| 2 | ReturnCode filterKeyValue(Cell v) | ReturnCode在Filter接口中定义的枚举类型,决定是否要包括该cell对象 (A way to filter based on the column family, column qualifier and/or the column value) |
| 3 | void filterRowCells(List<Cell> ignored) | 方法传入通过filterKeyValue的对象列表,然后在这里对列表里的元素进行任何转换或运算 |
| 4 | boolean filterRow() | 如果需要过滤掉某些行,那么返回true则过滤掉上面方法正在计算的行 |
| 5 | boolean filterAllRemaining() | 在过滤器里构建逻辑来提前停止一次扫描。 例如:在扫描很多行时,在行键、列限定符、单元值里找指定东西时,一旦找到目标,就不必关心剩下的行,可使用这个方法过滤 |
附:
filter执行流程(旧版):http://my.oschina.net/cloudcoder/blog/289649
旧版本的过滤方法 http://doc.okbase.net/wgp13x/archive/121557.html
示例代码:根据经纬度,过滤掉不在指定区域范围内的点:
参考材料:https://www.github.com/hbaseinaction
https://github.com/hbaseinaction/gis
import java.io.IOException;
import java.util.List; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.filter.FilterBase;
import org.apache.hadoop.hbase.util.Bytes; import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory; public class WithinFilter extends FilterBase { static final byte[] TABLE = "wifi".getBytes();
static final byte[] FAMILY = "a".getBytes();
static final byte[] ID = "id".getBytes();
static final byte[] X_COL = "lon".getBytes();
static final byte[] Y_COL = "lat".getBytes(); static final Log LOG = LogFactory.getLog(WithinFilter.class); final GeometryFactory factory = new GeometryFactory(); Geometry query = null;
boolean exclude = false; public WithinFilter() {
} public WithinFilter(Geometry query) {
this.query = query;
} //遍历每行每个列族的每个KeyValue的方法可能很慢,如果可以,HBase会优化对filterRow的调用
@Override
public boolean hasFilterRow(){
return true;
} //根据column family, column qualifier 或者 column value进行过滤
@Override
public ReturnCode filterKeyValue(Cell cell) throws IOException {
byte[] qualname = CellUtil.cloneQualifier(cell);
if(Bytes.equals(qualname, Bytes.toBytes("不需要的qualifier名"))) //例如可以处理密码,并且将密码跳过不反回到客户端
return ReturnCode.SKIP;
return ReturnCode.INCLUDE;
} //根据经纬度过滤,符合要求的为在区域内的点
@Override
public void filterRowCells(List<Cell> celllist) throws IOException{
double lon = Double.NaN;
double lat = Double.NaN;
for(Cell cell : celllist){
if(Bytes.equals(CellUtil.cloneQualifier(cell), X_COL)){
lon = Double.parseDouble(new String(CellUtil.cloneValue(cell)));
}
if(Bytes.equals(CellUtil.cloneQualifier(cell), Y_COL)){
lat = Double.parseDouble(new String(CellUtil.cloneValue(cell)));
}
}
Coordinate coord = new Coordinate(lon,lat);
Geometry point = factory.createPoint(coord); //创建Point
if(!query.contains(point)){ //测试是否包含该点
this.exclude = true;
}
} //如果某一行没有落在查询边界想要排除它是,需要设置exclude标志。
@Override
public boolean filterRow() {
if (LOG.isDebugEnabled())
LOG.debug("filter applied. " + (this.exclude ? "rejecting" : "keeping"));
return this.exclude;
} @Override
public void reset() {
this.exclude = false;
}
}
-------------------------------------------
HBase笔记--自定义filter的更多相关文章
- Hbase 学习(二)补充 自定义filter
本来这个内容是不单独讲的,但是因为上一个页面太大,导致Live Writer死机了,不能继续编辑了,所以就放弃了 这里要讲的是自定义filter,从FilterBase继承 public class ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第6节 SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener_24、深入SpringBoot过滤器和Servlet配置过滤器
笔记 1.深入SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter实战(核心知识) 简介:讲解SpringBoot里面Filter讲解和使用Servle ...
- Asp.net mvc自定义Filter简单使用
自定义Filter的基本思路是继承基类ActionFilterAttribute,并根据实际需要重写OnActionExecuting,OnActionExecuted,OnResultExecuti ...
- Jinja2模版语言自定义filter的使用
Jinja2模版语言,自带有一些filter,能够在前端的模版中控制数据按照相应的方式显示.比如以下两种filter,分别能在前端控制数字的近似精度显示和根据字符串长度补齐: round(value, ...
- Spring MVC 项目搭建 -6- spring security 使用自定义Filter实现验证扩展资源验证,使用数据库进行配置
Spring MVC 项目搭建 -6- spring security使用自定义Filter实现验证扩展url验证,使用数据库进行配置 实现的主要流程 1.创建一个Filter 继承 Abstract ...
- Spring-Security 自定义Filter完成验证码校验
Spring-Security的功能主要是由一堆Filter构成过滤器链来实现,每个Filter都会完成自己的一部分工作.我今天要做的是对UsernamePasswordAuthenticationF ...
- DirectX:在graph自动连线中加入自定义filter(graph中遍历filter)
为客户提供的视频播放的filter的测试程序中,采用正向手动连接的方式(http://blog.csdn.net/mao0514/article/details/40535791),由于不同的视频压缩 ...
- Spring Security 入门(1-6-2)Spring Security - 内置的filter顺序、自定义filter、http元素和对应的filterChain
Spring Security 的底层是通过一系列的 Filter 来管理的,每个 Filter 都有其自身的功能,而且各个 Filter 在功能上还有关联关系,所以它们的顺序也是非常重要的. 1.S ...
- Python学习(三十七)—— 模板语言之自定义filter和中间件
一.模板语言之自定义filter 自定义filter文件存放位置 模板中自定义函数 - 在已注册的app中创建一个名字叫 templatetags 文件夹 - 任意创建一个py文件 - 创建名字交 r ...
随机推荐
- 构建你的第一个App
Building Your First App 原文链接:http://developer.android.com/training/basics/firstapp/index.html 译文链接1: ...
- CSAPP:cachelab(1)
本项目大体上就是要求用C\C++来模拟cpu对cache的访问,然后统计hits.misses和eviction的次数.其实并没有想象中的那么难,感觉完全可以当成一道acm里面的大模拟题..下面就对这 ...
- org.apache.struts.chain.commands.InvalidPathException: No action config found for the specified url.
No action config found for the specified url url路径下找不到action,原因是stuts-config.xml文件配置错误. demo的项目文件如下: ...
- RELATED INTRODUCED
1.综合知识 RSS,IMDB 1.1什么是RSS? RSS(Really Simple Syndication)是一种描述和同步网站内容的格式,是目前使用最广泛的XML应用.RSS搭建了信息迅速传播 ...
- 在ios中解析json数据
刚刚下午那会 弄了个 解析 xml demo的小例子,本想着json也挺复杂 弄还是 不弄,但是简单的看了下 发现挺简单 考虑了很久,还是写上来吧,毕竟json用得太多了,而且算是自己的积累吧,毕竟 ...
- ASP.NET页面同时发起多个ajax请求,出现阻塞情况
解决办法:假如页面对Session只读不写,可以在页面的开头加上EnableSessionState="ReadOnly".
- ASP.NET MVC框架开发系列课程 (webcast视频下载)
课程讲师: 赵劼 MSDN特邀讲师 赵劼(网名“老赵”.英文名“Jeffrey Zhao”,技术博客为http://jeffreyzhao.cnblogs.com),微软最有价值专家(ASP.NET ...
- NDK GDB 中打印vector , vector<vector <> >
在android上进行native开发的时候,我们需要用NDK-GDB 对native code进行调试,其中很麻烦的是,我使用的NDK版本是4.0,该版本还不支持用NDK-GDB直接打印vector ...
- PowerDesigner 生成带凝视SQL 各个版本号通用10(12、15)
做数据库是设计时最苦恼的事就是用PowerDesigner工具设计完数据库运行SQL文件后没有凝视.那么怎么才干让PowerDesigner设计完有凝视呢,下边教你一个笨的方法,方法尽管笨,可是能实现 ...
- java 类加载过程
1. 使用命令行查看类加载过程,在eclipse测试类的run configuration中配置-verbose:class或者-verbose,如下图所示: 运行结果如下所示: [Opened D: ...