【HBase】Java实现过滤器查询
目录
概述
过滤器可以分为两种:比较过滤器和专用过滤器。过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端。
比较过滤器
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实现过滤器查询的更多相关文章
- HBase根据Rowkey批量查询数据JAVA API(一次查多条,返回多个记录)
最近在生产中遇到了一个需求,前台给我多个rowkey的List,要在hbase中查询多个记录(返回给前台list).在网上也查了很多,不过自己都不太满意,filter的功能有可能查询结果不是准确值,而 ...
- Hbase Filter过滤器查询详解
过滤器查询 引言:过滤器的类型很多,但是可以分为两大类——比较过滤器,专用过滤器 过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端: hbase过滤器的比较运算符: LE ...
- hbase java 基本例子
一下代码实用 0.99.0 以后的版本. package hadoop; import java.io.IOException; import java.util.ArrayList; import ...
- 项目使用Hbase进行数据快速查询的代码案例
之前项目中对于数据详情的查询使用的ddb技术,由于成本过高,现考虑使用开源的hbase框架,借此机会进行hbase的代码案例记录,之前已经对 hbase的原理进行介绍,介绍了hbase中的rowkey ...
- HBase笔记6 过滤器
过滤器 过滤器是GET或者SCAN时过滤结果用的,相当于SQL的where语句 HBase中的过滤器创建后会被序列化,然后分发到各个region server中,region server会还原过滤器 ...
- hbase基于hue的查询语法
hbase基于hue的查询语法 登录地址 https://hue-ui.xiaoniangao.cn 界面操作说明 进入hue中的hbase 进入表的查询界面 界面说明 查询语句 ,表示结束查询,可以 ...
- java编码过滤器
1.java编码过滤器的作用: java过滤器能够对目标资源的请求和响应进行截取,过滤信息执行的优先级高于servlet. 2.java过滤器的使用: (1)编写一个普通的java类,实现Filter ...
- Hibernate之HQL添加过滤器查询的用法
HQL查询过程中支持添加过滤器.使用步骤是这样的: 首先在要查询的实体对象的映射中使用<filter-def>标签配置过滤器,并在相对应的<class>标签中添加对应的< ...
- Java内部DNS查询实现和参数设置
一.Java内部DNS查询 Java使用域名查询时,用的自己内部的域名实现机制,最后都是交给InetAddress去做DNS解析. 源码分析参考:http://blog.arganzheng.me/p ...
随机推荐
- python批量爬取动漫免费看!!
实现效果 运行环境 IDE VS2019 Python3.7 Chrome.ChromeDriver Chrome和ChromeDriver的版本需要相互对应 先上代码,代码非常简短,包含空行也才50 ...
- 【selenium】各种exception
selenium中的Exception解释 exception selenium.common.exceptions.ElementClickInterceptedException(msg=None ...
- 小L的直线
小学时期的小L发现自己很有艺术细胞,于是买了一块画板,但是他的绘画水平使得他只能连接两点画出一条线段.有一天他决定在一张有n个点的图上作画,即他可以把这n个点任意连接.大家认为平行线是非常不美观的,于 ...
- jarvisoj MISC 取证2
打开之后一个文件和一个镜像 TrueCrypt....记住他了,再看一眼那个文件,好的,TrueCrypt加密..找密码 把Truecrypt.exe直接dump下来,用efdd解密就行了
- SpringCloud(七)超时、重试
一.Ribbon(单独配置) 可以通过ribbon.xx来进行全局配置.也可以通过服务名.ribbon.xx来对指定服务配置 全局配置: ribbon: ConnectTimeout: 3000 #连 ...
- Python常见报错 - 使用openpyxl模块时出现错误: zipfile.BadZipFile: File is not a zip file
背景 在pycharm项目下,有一个data.xlsx,主要用来存放接口测试用例数据的 要通过openpyxl库去读取data.xlsx,方法: openpyxl.load_workbook(path ...
- 在 Azure CentOS VM 中配置 SQL Server 2019 AG - (上)
前文 假定您对Azure和SQL Server HA具有基础知识 假定您对Azure Cli具有基础知识 目标是在Azure Linux VM上创建一个具有三个副本的可用性组,并实现侦听器和Fenci ...
- JavaScript type="text/template"的用法
JavaScript type="text/template"相当于定义一个模板,如果没有使用html()方法的话,是显示不出来的,我们直接看例子(我是在tp框架的里面写的) &l ...
- centos7下端口映射
firewall-cmd --zone=external --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.10.10 --perma ...
- awk,seq,xarg实例使用
基于https://www.cnblogs.com/wangyuebo/p/5836933.html的详细补充讲解 [root@localhost awk]# seq 10|xargs -n 2 &g ...