1、首先生成自定义过滤器,生成jar包,然后拷贝到服务器hbase目录的lib下。

1.1 自定义过滤器CustomFilter

import com.google.protobuf.InvalidProtocolBufferException;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.exceptions.DeserializationException;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterBase;
import org.apache.hadoop.hbase.util.ByteStringer; import java.io.IOException; /**
* @Author: xu.dm
* @Date: 2019/4/14 12:16
* @Description: 自定义过滤器,选择列值匹配的行数据
*/
public class CustomFilter extends FilterBase {
private byte[] value = null;
private boolean filterRow = true; public CustomFilter() {
super();
} public CustomFilter(byte[] value) {
this.value = value;
} @Override
public void reset() throws IOException {
this.filterRow = true;
} @Override
public boolean filterRow() throws IOException {
return this.filterRow;
} //匹配的数据不过滤
@Override
public ReturnCode filterCell(Cell c) throws IOException {
if(CellUtil.matchingValue(c,value))
filterRow = false;
return ReturnCode.INCLUDE;
} /**
* protobuf生成MyFilterProtos
*/
@Override
public byte[] toByteArray() throws IOException {
MyFilterProtos.CustomFilter.Builder builder = MyFilterProtos.CustomFilter.newBuilder();
if(value!=null)
builder.setValue(ByteStringer.wrap(value)); return builder.build().toByteArray();
} public static Filter parseFrom(final byte[] pbBytes)
throws DeserializationException {
MyFilterProtos.CustomFilter proto;
try {
proto = MyFilterProtos.CustomFilter.parseFrom(pbBytes);
} catch (InvalidProtocolBufferException e) {
throw new DeserializationException(e);
}
return new CustomFilter(proto.getValue().toByteArray());
}
}

1.2 MyFilterProtos是通过protobuf生成的,这里需要注意hbase使用的是protobuf2.5.0版本,不要使用高于2.5.0版本的protobuf,不然hbase会报找不到类的错误。

proto文件

syntax = "proto2";

option java_package = "";
option java_outer_classname = "MyFilterProtos";
option java_generic_services = true;
option java_generate_equals_and_hash = true;
option optimize_for = SPEED; message CustomFilter {
required bytes value = 1;
}

1.3 编辑成jar包,发布到hbase的lib目录,hbase需要重启

[root@bigdata-senior01 lib]# ls $HBASE_HOME/lib/Custom*.*
/opt/hbase-2.0.4/lib/CustomFilter.jar

2、使用自定义过滤

2.1 程序中引入刚才发布的jar包

2.2 使用自定义过滤器过滤数据

   //使用自定义过滤器,只显示匹配列值的行
private static void customFilterData() throws IOException{
Table table = helper.getConnection().getTable(TableName.valueOf("testtable")); List<Filter> filters = new ArrayList<Filter>(); Filter filter1 = new CustomFilter(Bytes.toBytes("user30"));
filters.add(filter1); Filter filter2 = new CustomFilter(Bytes.toBytes("user20"));
filters.add(filter2); Filter filter3 = new CustomFilter(Bytes.toBytes("user90"));
filters.add(filter3); FilterList filterList = new FilterList(
FilterList.Operator.MUST_PASS_ONE, filters); Scan scan = new Scan();
scan.setFilter(filterList);
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner){
helper.dumpResult(result);
} scanner.close();
table.close();
}
}

输出结果:

Cell: rowKey20/ex:addr/1555078771906/Put/vlen=8/seqid=0, Value: street20
Cell: rowKey20/info:username/1555078771906/Put/vlen=6/seqid=0, Value: user20
Cell: rowKey20/memo:detail/1555078771906/Put/vlen=8/seqid=0, Value: remark20
Cell: rowKey30/ex:addr/1555078771906/Put/vlen=8/seqid=0, Value: street30
Cell: rowKey30/info:username/1555078771906/Put/vlen=6/seqid=0, Value: user30
Cell: rowKey30/memo:detail/1555078771906/Put/vlen=8/seqid=0, Value: remark30
Cell: rowKey90/ex:addr/1555078771906/Put/vlen=8/seqid=0, Value: street90
Cell: rowKey90/info:username/1555078771906/Put/vlen=6/seqid=0, Value: user90
Cell: rowKey90/memo:detail/1555078771906/Put/vlen=8/seqid=0, Value: remark90

hbase 自定义过滤器的更多相关文章

  1. HBase - Filter - 过滤器的介绍以及使用

    1 过滤器HBase 的基本 API,包括增.删.改.查等.增.删都是相对简单的操作,与传统的 RDBMS 相比,这里的查询操作略显苍白,只能根据特性的行键进行查询(Get)或者根据行键的范围来查询( ...

  2. 实现MVC自定义过滤器,自定义Area过滤器,自定义Controller,Action甚至是ViewData过滤器

    MVC开发中几种以AOP方式实现的Filters是非常好用的,默认情况下,我们通过App_Start中的FilterConfig来实现的过滤器注册是全局的,也就是整个应用程序都会使用的,针对单独的Fi ...

  3. lucene自定义过滤器

    先介绍下查询与过滤的区别和联系,其实查询(各种Query)和过滤(各种Filter)之间非常相似,可以这样说只要用Query能完成的事,用过滤也都可以完成,它们之间可以相互转换,最大的区别就是使用过滤 ...

  4. asp.net MVC之 自定义过滤器(Filter) - shuaixf

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...

  5. angular之自定义过滤器的使用

    自定义过滤器需要使用filter函数,格式如下: filter("filterName',function(){ return function(target,args){ .... } } ...

  6. 第六节:Vue过滤器的用法和自定义过滤器

    1.过滤器的用法,用  '|' 分割表达式和过滤器. 例如:{{ msg |  filter}}     {{msg | filter(a)}}  a就标识filter的一个参数. 用两个过滤器:{{ ...

  7. .net中自定义过滤器对Response内容进行处理

    原文:http://www.cnblogs.com/zgqys1980/archive/2008/09/02/1281895.html 代码DEMO:http://files.cnblogs.com/ ...

  8. asp.net MVC之 自定义过滤器(Filter)

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...

  9. Vue自定义过滤器

    gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson05 一 自定义过滤器(注册在Vue全局) 注意事项: (1)全局方 ...

随机推荐

  1. ubuntu下安装pip install mysqlclient 报错 command "python setup.py egg_info" failed with error.....解决方案

    我的环境: ubuntu 1604 版本, 在黑屏终端已经安装了django和virtualenv虚拟环境, 在创建了django的models后开始迁移的操作, 出现错误, 错误代码最后如题目 可以 ...

  2. allure2 report+ jenkins 使用

    物色了一个挺漂亮的报告生成插件 ——allure. 下面介绍一下这个报告的使用. 1. 添加依赖 <dependencies> <!-- https://mvnrepository. ...

  3. [环境配置]Ubuntu 16.04+CUDA 9.0+OpenCV 3.2.0下编译基于Caffe的MobileNet-SSD踩过的一些坑

    SSD是Caffe的一个分支,源码在github上:https://github.com/weiliu89/caffe/tree/ssd $ git clone https://github.com/ ...

  4. 《数据结构与算法图解》 分享 pdf下载

    链接:https://pan.baidu.com/s/1gOMlwU5ucHYDVazvVMk2uw提取码:bk5x

  5. OpenGL学习笔记(2) 画一个正方形

    画一个正方形 其实,画正方形就是画两个三角形,用四个顶点以及使用索引来实现 完整代码在Square项目的Application.cpp里 先贴上窗口初始化代码 void BaseInit() { gl ...

  6. node安装和npm全局配置

    本文章环境 windows10 64位家庭版 Node10.15.3LTS 安装包下载 Node官网 安装node 点击安装文件, 一键安装, 注意安装位置和添加到环境变量(xx to PATH)选项 ...

  7. 使用tensorflow进行mnist数字识别【模型训练+预测+模型保存+模型恢复】

      import sys,os sys.path.append(os.pardir) import numpy as np from tensorflow.examples.tutorials.mni ...

  8. virtual box下安装ubuntu经验

    1. 哪怕下载的是ubuntu64位版本,也在vitualbox下选择ubuntu而不要选择ubuntu(64bit) 2. 安装VBoxGuestAdditional.iso:下载和vbox版本相匹 ...

  9. yum安装lnmp

    python其他知识目录 1.安装LNMP之前要安装EPEL,以便安装源以外的软件,如Nginx,phpMyAdmin等. yum install epel-release 提示:EPEL,即Extr ...

  10. IDEA2017.3.4破解方式及lombok图文配置详解

    下载jetbrainsCrack-2.7-release-str.jar包 下载地址: https://files.cnblogs.com/files/xifenglou/JetBrains.zip ...