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 ...
随机推荐
- 【转】android蓝牙开发---与蓝牙模块进行通信--不错
原文网址:http://www.cnblogs.com/wenjiang/p/3200138.html 近半个月来一直在搞android蓝牙这方面,主要是项目需要与蓝牙模块进行通信.开头的进展很顺利, ...
- Palindrome Partitioning——LeetCode
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 《算法问题实战策略》-chaper13-数值分析
这一章节主要介绍我们在进行数值分析常用的二分.三分和一个近似求解区间积分的辛普森法. 首先介绍二分. 其实二分的思想很好理解并且笔者在之前的一些文章中也有所渗透,对于二次函数甚至单元高次函数的零点求解 ...
- Android Develop 之 Ddevelop WorkFlow Basics
Android应用程序的开发流程一言以蔽之,可以用一图五步概括.一图如下. 第一步:安装.安装开发环境,包括JDK,Android Studio,SDK Manager(通常下载Android Stu ...
- Robot Framework安装配置 Linux
Simple introduction Robot Framework is a generic test automation framework for acceptance testing an ...
- GMU
NEXT | 不错过任何一个新产品 百度开源 Mobile UI 组件库,提供 Web app.Pad 端简单易用的 UI 组件 发表评论
- java 中流的使用
Java中的流,可以从不同的角度进行分类. 一.按照数:输入流和输出流. 输出流: 输入流: 因此输入和输出都是从程序的角度来说的. 二.按照处理数据单位不同可以分为:字节流和字符流. 字节流和字符流 ...
- Android Studio常用插件
ButterKnife 这个开源库可以让我们从大量的findViewById()和setonclicktListener()解放出来,其对性能的影响微乎其微(查看过Butter Knife的源码,其自 ...
- Drawable 着色的后向兼容方案
看到 Android Weekly 最新一期有一篇文章:Tinting drawables,使用 ColorFilter 手动打造了一个TintBitmapDrawable,之前也看到有些文章使用这种 ...
- state模式理解
state模式应用场景 条件判断很多的情况 比如有很多if else语句:switch case语句等等. 如果以后业务越来越复杂,条件判断有100多个,每种条件的处理逻辑很复杂,不止一个业务逻辑会重 ...