概述

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

  • 比较过滤器

    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. 利用opencv实现视频捕捉功能

    import cv2 as cv import numpy as np def video_demo(): capture = cv.VideoCapture(0) #打开摄像头,参数0代表设备ID( ...

  2. Threejs【坐标转换】如何让annotation跟随物体一起旋转

    现在根据鼠标点击的屏幕位置能够得到屏幕的坐标event.clientX和event.clientY,然后我的annotation就初始化在这个屏幕坐标的位置,那么如何绑定annotation和三维物体 ...

  3. Ant概念

    Ant是基于Java的.可以跨平台的项目编译和生成工具.

  4. A 蚂蚁觅食

    A. 蚂蚁觅食(一) 单点时限: 1.0 sec 内存限制: 512 MB 一只饥饿的小蚂蚁外出觅食,幸运的小蚂蚁发现了好多食物,但是它只有一次搬食物的机会.可因为力气太小了,它不能搬走重量超过自己体 ...

  5. 详解 Properties类

    (请观看本人博文--<详解 I/O流>) Properties类: 概念: Properties 类的对象 是 一个持久的属性集 Properties 可 保存在流中 或 从流中加载 属性 ...

  6. Everything信息泄露

    Everything漏洞描述 [Everything]一款搜索文件非常快的工具,其速度之快令人震惊!它还有一个可以通过HTTP 或 FTP 分享搜索结果 的功能.它可以让用户在本地或局域网上的其他电脑 ...

  7. awd平台搭建

    1.先是使用 https://github.com/m0xiaoxi/AWD_CTF_Platform 这个平台搭建 这个平台很好用,是python脚本自动搭建,基本不需要怎么更改,自带了四道题的源码 ...

  8. node 搭载本地代理,处理web本地开发跨域问题

    var path = require('path') var httpProxy = require('http-proxy') var express = require('express') va ...

  9. php中&&和and有什么区别

    PHP中的逻辑“与”运算有两种形式:AND 和 &&,同样“或”运算也有OR和||两种形式. 如果是单独两个表达式参加的运算,两种形式的结果完全相同,例如 $a AND $b和$a & ...

  10. 我们常听到的WAL到底是什么

    什么是 WAL WAL(Write Ahead Log)预写日志,是数据库系统中常见的一种手段,用于保证数据操作的原子性和持久性. 在计算机科学中,预写式日志(Write-ahead logging, ...