概述

过滤器可以分为两种:比较过滤器专用过滤器过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端。

  • 比较过滤器

    LESS —— 小于

    LESS_OR_EQUAL —— 小于等于

    EQUAL —— 等于

    NOT_EQUAL —— 不等于

    GREATER_OR_EQUAL —— 大于等于

    GREATER —— 大于

    NO_OP —— 排除所有

  • 专用过滤器

    BinaryComparator —— 按字节索引顺序比较指定字节数组,采用Bytes.compareTo(byte[])

    BinaryPrefixComparator —— 跟前面相同,只是比较左端的数据是否相同

    NullComparator —— 判断给定的是否为空

    BitComparator —— 按位比较

    RegexStringComparator —— 提供一个正则的比较器,仅支持 EQUAL 和非EQUAL

    SubstringComparator —— 判断提供的子串是否出现在value中


代码实现

以下代码均有一个BeforeTest和一个AfterTest

/**
* 创建连接HBase服务器的方法
*/
private Connection connection;
private Table table; @BeforeTest
public void init() throws IOException {
//获取连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
connection = ConnectionFactory.createConnection(configuration);
//获取表
table = connection.getTable(TableName.valueOf("myuser"));
}
/**
* 关闭系统连接和表连接
*/
@AfterTest
public void close() throws IOException {
//关闭表
table.close();
connection.close();
}

rowKey过滤器RowFilter

/**
* 过滤rowKey比0003小的数据
*/
@Test
public void rowFilter() throws IOException {
Scan scan = new Scan(); //因为RowFilter需要一个binaryComparator参数,所以需要创建一个对象
BinaryComparator binaryComparator = new BinaryComparator("0003".getBytes()); //通过rowFilter按照rowKet进行过滤
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, binaryComparator); scan.setFilter(rowFilter); ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row)); List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}

列族过滤器FamilyFilter

/**
* 列族过滤器,只获取f2列族的数据
*/
@Test
public void familyFilter() throws IOException {
//获取Scan对象
Scan scan = new Scan(); //FamilyFilter需要一个binaryComparator的参数,所以新建一个对象
BinaryComparator binaryComparator = new BinaryComparator("f2".getBytes());
//scan.setFilter需要的参数是FamilyFilter
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, binaryComparator); //数据装在scan中
scan.setFilter(familyFilter); //拿到需要的所有数据
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row)); List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}

列过滤器QualifierFilter

/**
* 列过滤器,只查询name这一列
*/
@Test
public void qualifierFilter() throws IOException {
//获取Scan对象
Scan scan = new Scan();
//列过滤器就是QualifierFilter,new一个
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("name"));
//将列过滤器的值设置到scan中
scan.setFilter(qualifierFilter);
//获取到所有的scan的数据
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}

列值过滤器ValueFilter

    /**
* 列值过滤器,查询列中含有8的值
*/
@Test
public void valueFilter() throws IOException {
//获取Scan对象
Scan scan = new Scan();
//列值过滤器ValueFilter,new一个对象
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("8"));
//将ValueFilter过滤后的值放到Scan中
scan.setFilter(valueFilter);
//获取所有数据
ResultScanner resultScanner = table.getScanner(scan);
//遍历循环得到每一条数据
for (Result result : resultScanner) {
//获取每一条数据的rowKey
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
//获取到数据中的每一个cell
List<Cell> cells = result.listCells();
//遍历循环得到每一个cell的值
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}

专用过滤器

单列值过滤器 SingleColumnValueFilter

    /**
* 单列值过滤器,查询name为刘备的人
*/
@Test
public void singleColumnValueFilter() throws IOException {
//new一个Scan对象
Scan scan = new Scan();
//单列值过滤器,new一个对象,来限定条件
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
//获取限定条件后拿到的数据
scan.setFilter(singleColumnValueFilter);
//所有数据封装到resultScanner中
ResultScanner resultScanner = table.getScanner(scan);
//循环遍历resultScanner中的每条数据
for (Result result : resultScanner) {
//获取每条数据的rowKey值
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
//获取每条数据中的cell值
List<Cell> cells = result.listCells();
//遍历循环得到每一个cell
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
} }

列值排除过滤器SingleColumnValueExcludeFilter

    /**
* 列值排除过滤器
*/
@Test
public void singleColumnValueExcludeFilter() throws IOException {
Scan scan = new Scan();
scan.setFilter(new SingleColumnValueExcludeFilter("f1".getBytes(),"name".getBytes(), CompareFilter.CompareOp.EQUAL,"刘备".getBytes()));
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
} }

rowKey前缀过滤器PrefixFilter

/**
* row前缀过滤器,查询以00开头的所有前缀的rowkey
*/
@Test
public void prefixFilter() throws IOException {
Scan scan = new Scan();
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
scan.setFilter(prefixFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}

&160;

分页过滤器PageFilter

    /**
* 分页过滤器
* 分页有两个条件
* pageNum 第几页
* pageSize 每页有几条
*/
@Test
public void pageFilter() throws IOException { int pageNum = 3;
int pageSize = 2; /*
分为两种情况判断:
第一页
其他页
*/
if (pageNum == 1){
Scan scan = new Scan();
//设置起始rowKey
scan.setStartRow("".getBytes());
//设置最大的返回结果,返回pageSize条
scan.setMaxResultSize(pageSize);
//分页过滤器
PageFilter pageFilter = new PageFilter(pageSize);
scan.setFilter(pageFilter); ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
} else {
String startRow = "";
Scan scan = new Scan();
/*
第二页的起始rowKey = 第一页的结束rowKey + 1
第三页的起始rowKey = 第二页的结束rowKey + 1
*/
int resultSize = (pageNum - 1) * pageSize + 1;
scan.setMaxResultSize(resultSize);
//设置一次性往前扫描5条,最后一个rowKey是第三页起始rowKey
PageFilter pageFilter = new PageFilter(resultSize);
scan.setFilter(pageFilter);
//resultScanner里面有5条数据
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
//获取rowKey
byte[] row = result.getRow();
//最后一次循环遍历 rowKey为0005
startRow = Bytes.toString(row);
}
Scan scan1 = new Scan();
scan1.setStartRow(startRow.getBytes());
scan1.setMaxResultSize(pageSize); PageFilter pageFilter1 = new PageFilter(pageSize);
scan1.setFilter(pageFilter1); ResultScanner scanner1 = table.getScanner(scan1);
for (Result result : scanner1) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}
}

多过滤器综合查询FilterList

需求: 使用 SingleColumnValueFilter 查询f1列族,name为刘备的数据,并且同时满足rowkey的前缀以00开头的数据(PrefixFilter)

    /**
* 多过滤综合查询
* 需求: 使用 SingleColumnValueFilter 查询f1列族,name为刘备的数据,并且同时满足rowkey的前缀以00开头的数据(PrefixFilter)
*/
@Test
public void filterList() throws IOException {
Scan scan = new Scan(); SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(),"name".getBytes(), CompareFilter.CompareOp.EQUAL,"刘备".getBytes());
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes()); FilterList filterList = new FilterList(singleColumnValueFilter, prefixFilter); scan.setFilter(filterList); ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("数据的rowKey为" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
byte[] family = cell.getFamily();
//id列和age列是整型数据
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
} else {
System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
}
}
}
}

代码实现删除数据

	/**
* 根据rowKey删除数据
*/
@Test
public void delete() throws IOException {
Delete delete = new Delete("0007".getBytes()); table.delete(delete); }

【HBase】Java实现过滤器查询的更多相关文章

  1. HBase根据Rowkey批量查询数据JAVA API(一次查多条,返回多个记录)

    最近在生产中遇到了一个需求,前台给我多个rowkey的List,要在hbase中查询多个记录(返回给前台list).在网上也查了很多,不过自己都不太满意,filter的功能有可能查询结果不是准确值,而 ...

  2. Hbase Filter过滤器查询详解

    过滤器查询 引言:过滤器的类型很多,但是可以分为两大类——比较过滤器,专用过滤器 过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端: hbase过滤器的比较运算符: LE ...

  3. hbase java 基本例子

    一下代码实用  0.99.0 以后的版本. package hadoop; import java.io.IOException; import java.util.ArrayList; import ...

  4. 项目使用Hbase进行数据快速查询的代码案例

    之前项目中对于数据详情的查询使用的ddb技术,由于成本过高,现考虑使用开源的hbase框架,借此机会进行hbase的代码案例记录,之前已经对 hbase的原理进行介绍,介绍了hbase中的rowkey ...

  5. HBase笔记6 过滤器

    过滤器 过滤器是GET或者SCAN时过滤结果用的,相当于SQL的where语句 HBase中的过滤器创建后会被序列化,然后分发到各个region server中,region server会还原过滤器 ...

  6. hbase基于hue的查询语法

    hbase基于hue的查询语法 登录地址 https://hue-ui.xiaoniangao.cn 界面操作说明 进入hue中的hbase 进入表的查询界面 界面说明 查询语句 ,表示结束查询,可以 ...

  7. java编码过滤器

    1.java编码过滤器的作用: java过滤器能够对目标资源的请求和响应进行截取,过滤信息执行的优先级高于servlet. 2.java过滤器的使用: (1)编写一个普通的java类,实现Filter ...

  8. Hibernate之HQL添加过滤器查询的用法

    HQL查询过程中支持添加过滤器.使用步骤是这样的: 首先在要查询的实体对象的映射中使用<filter-def>标签配置过滤器,并在相对应的<class>标签中添加对应的< ...

  9. Java内部DNS查询实现和参数设置

    一.Java内部DNS查询 Java使用域名查询时,用的自己内部的域名实现机制,最后都是交给InetAddress去做DNS解析. 源码分析参考:http://blog.arganzheng.me/p ...

随机推荐

  1. Python-气象-大气科学-可视化绘图系列(二)——利用basemap叠加地图,并添加白化效果(代码+示例)

    本文为原创链接: https:////www.cnblogs.com/zhanling/p/12193031.html 白化单图代码: import numpy as np import xarray ...

  2. 胜利大逃亡 BFS

    Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0 ...

  3. Windows Pains poj 2585

    Boudreaux likes to multitask, especially when it comes to using his computer. Never satisfied with j ...

  4. 原创zookeeper3.4.6集群安装

    tar -zxvf zookeeper-3.4.6.tar.gz -C /home/hadoop/ vi ~/.bash_profile export ZOOKEEPER_HOME=/home/had ...

  5. frame/iframe多表单切换

    应用场景: 在Web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe表单内嵌页面上的元素无法直接定位.这时就 ...

  6. SUCTF 2019 Upload labs 2 踩坑记录

    SUCTF 2019 Upload labs 2 踩坑记录 题目地址 : https://github.com/team-su/SUCTF-2019/tree/master/Web/Upload La ...

  7. Spiking-YOLO : 前沿性研究,脉冲神经网络在目标检测的首次尝试 | AAAI 2020

    论文提出Spiking-YOLO,是脉冲神经网络在目标检测领域的首次成功尝试,实现了与卷积神经网络相当的性能,而能源消耗极低.论文内容新颖,比较前沿,推荐给大家阅读   来源:晓飞的算法工程笔记 公众 ...

  8. 进阶 Linux基本命令-1

    vmware三种网络模式1,桥接虚拟机直接连接外网,局域网.宿主机电脑不提供路由. 2,NAT网络地址转换,家庭网 3,host only 只能和宿主电脑打交道 Linux命令形式 命令 +[参数]+ ...

  9. IntelliJ IDEA在mac中完全删除方法

    cd /Applications/ rm -r IntelliJ\ IDEA.app/ rm -r /Users/apple/Library/Logs/IntelliJIdea2019.3/ rm - ...

  10. 关于宝塔下的项目中的php不能访问的问题

    遇到的问题是访问项目根目录的所有php文件都是报404错,而其他文件则可以,比如txt,最后查资料发现 在宝塔运行网站的时候会在项目的根目录自动生成一个.user.ini文件,这个文件主要是防止对指定 ...