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 ...
随机推荐
- SQL SELECT INTO 语句
SQL SELECT INTO 语句可用于创建表的备份复件. SELECT INTO 语句 SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中. SELECT INTO 语句常用 ...
- Sum Root to Leaf Numbers——LeetCode
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【索引】XBox360玩机心得
基础知识 查看XBox360的系统版本信息:http://www.cnblogs.com/duxiuxing/p/4292140.html XBox360光盘游戏的安装: 自制系统 XBox360自制 ...
- C#中DataTable行转列示例
将下面表(1)格式的数据转换为表(2)格式的数据.很明显,这是一个行转列的要求,本想在数据库中行转列,因为在数据库中行转列是比较简单的,方法可以参考本站SQLServer中(行列转换)行转列及列转行且 ...
- html5中input新增type值的使用
二狗在最近的项目以html5和webapp为主,并接触到了input新增type值的使用,下面就把这些玩意一一以实例列举出来 一:type = date:定义日期:年-月-日 input type=& ...
- 机器学习实战__KNN1
KNN的算法工作原理: 存在一个训练样本集合,样本集中每个数据都有确定的标签(分类),即我们知道样本集中每一数据与所属分类的对应关系.输人没有标签的新数据后,将新数据的每个特征与样本集中数据对应的特征 ...
- Java中equals和“==””的区别,String特殊
public class TestString { /* * java中的数据类型,可分为两类: * 1.基本数据类型,也称为原始数据类型.byte,short,char,int,long,float ...
- linux中内核的一个不错的参数somaxconn
导读:在linux中,/proc/sys/net/core/somaxconn这个参数,linux中内核的一个不错的参数somaxconn 看下其解析: 对于一个TCP连接,Server与Client ...
- css之z-index深度解析
(几个重点概念解析) 一.层叠上下文 层叠上下文:如果一个元素拥有层叠上下文.那么就代表这个元素在页面的z轴上是有定位的. 什么元素拥有 层叠上下文:a.根元素.b.z-index值为数值的元素.c. ...
- python运行时间计算之timeit
timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000) stmt:statement ...