HBase Filter 过滤器之QualifierFilter详解
前言:本文详细介绍了 HBase QualifierFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考。QualifierFilter 基于列名进行过滤,在工作中涉及到需要通过HBase 列名进行数据过滤时可以考虑使用它。比较器细节及原理请参照之前的更文:HBase Filter 过滤器之比较器 Comparator 原理及源码学习
一。Java Api
头部代码
/**
* 用于列名(Qualifier)过滤。
*/
public class QualifierFilterDemo {
private static boolean isok = false;
private static String tableName = "test";
private static String[] cfs = new String[]{"f"};
private static String[] data = new String[]{
"row-1:f:name:Wang", "row-1:f:age:20",
"row-2:f:name:Zhou", "row-2:f:age:10",
"row-3:f:gender:男", "row-3:f:name:Li",
"row-4:f:namana:xyz", "row-4:f:age:Zhao"
};
public static void main(String[] args) throws IOException {
MyBase myBase = new MyBase();
Connection connection = myBase.createConnection();
if (isok) {
myBase.deleteTable(connection, tableName);
myBase.createTable(connection, tableName, cfs);
// 造数据
myBase.putRows(connection, tableName, data);
}
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
中部代码
向右滑动滚动条可查看输出结果。
1. BinaryComparator 构造过滤器
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("age"))); // [row-1:f:age, row-2:f:age, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("name"))); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("gender"))); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("name"))); // [row-1:f:name, row-2:f:name, row-3:f:name]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("gender"))); // [row-1:f:age, row-2:f:age, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("gender"))); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
2. BinaryPrefixComparator 构造过滤器
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("nam"))); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("nam"))); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.GREATER, new BinaryPrefixComparator(Bytes.toBytes("g"))); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("n"))); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.LESS, new BinaryPrefixComparator(Bytes.toBytes("m"))); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("a"))); // [row-1:f:age, row-2:f:age, row-4:f:age]
3. SubstringComparator 构造过滤器
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("g")); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("n")); // [row-1:f:age, row-2:f:age, row-4:f:age]
4. RegexStringComparator 构造过滤器
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.NOT_EQUAL, new RegexStringComparator("nam")); // [row-1:f:age, row-2:f:age, row-3:f:gender, row-4:f:age]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("nam")); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("n[a-z]m")); // [row-1:f:name, row-2:f:name, row-3:f:name, row-4:f:namana]
尾部代码
scan.setFilter(qualifierFilter);
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
LinkedList<String> rowkeys = new LinkedList<>();
while (iterator.hasNext()) {
Result result = iterator.next();
String rowkey = Bytes.toString(result.getRow());
rowkeys.add(rowkey);
}
System.out.println(rowkeys);
scanner.close();
table.close();
connection.close();
}
}
二。Shell Api
1. BinaryComparator 构造过滤器
方式一:
hbase(main):003:0> scan 'test',{FILTER=>"QualifierFilter(=,'binary:age')"}
ROW COLUMN+CELL
row-1 column=f:age, timestamp=1589252853542, value=20
row-2 column=f:age, timestamp=1589252853542, value=10
row-4 column=f:age, timestamp=1589252853542, value=Zhao
3 row(s) in 0.0680 seconds
支持的比较运算符:= != > >= < <=,不再一一举例。
方式二:
import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.BinaryComparator
import org.apache.hadoop.hbase.filter.QualifierFilter
hbase(main):010:0> scan 'test',{FILTER => QualifierFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryComparator.new(Bytes.toBytes('age')))}
ROW COLUMN+CELL
row-1 column=f:age, timestamp=1589252853542, value=20
row-2 column=f:age, timestamp=1589252853542, value=10
row-4 column=f:age, timestamp=1589252853542, value=Zhao
3 row(s) in 0.0400 seconds
支持的比较运算符:LESS、LESS_OR_EQUAL、EQUAL、NOT_EQUAL、GREATER、GREATER_OR_EQUAL,不再一一举例。
推荐使用方式一,更简洁方便。
2. BinaryPrefixComparator 构造过滤器
方式一:
hbase(main):011:0> scan 'test',{FILTER=>"QualifierFilter(=,'binaryprefix:nam')"}
ROW COLUMN+CELL
row-1 column=f:name, timestamp=1589252853542, value=Wang
row-2 column=f:name, timestamp=1589252853542, value=Zhou
row-3 column=f:name, timestamp=1589252853542, value=Li
row-4 column=f:namana, timestamp=1589252853542, value=xyz
4 row(s) in 0.0410 seconds
方式二:
import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator
import org.apache.hadoop.hbase.filter.QualifierFilter
hbase(main):014:0> scan 'test',{FILTER => QualifierFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryPrefixComparator.new(Bytes.toBytes('nam')))}
ROW COLUMN+CELL
row-1 column=f:name, timestamp=1589252853542, value=Wang
row-2 column=f:name, timestamp=1589252853542, value=Zhou
row-3 column=f:name, timestamp=1589252853542, value=Li
row-4 column=f:namana, timestamp=1589252853542, value=xyz
4 row(s) in 0.0200 seconds
其它同上。
3. SubstringComparator 构造过滤器
方式一:
hbase(main):015:0> scan 'test',{FILTER=>"QualifierFilter(=,'substring:am')"}
ROW COLUMN+CELL
row-1 column=f:name, timestamp=1589252853542, value=Wang
row-2 column=f:name, timestamp=1589252853542, value=Zhou
row-3 column=f:name, timestamp=1589252853542, value=Li
row-4 column=f:namana, timestamp=1589252853542, value=xyz
4 row(s) in 0.0230 seconds
方式二:
import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.SubstringComparator
import org.apache.hadoop.hbase.filter.QualifierFilter
hbase(main):017:0> scan 'test',{FILTER => QualifierFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), SubstringComparator.new('am'))}
ROW COLUMN+CELL
row-1 column=f:name, timestamp=1589252853542, value=Wang
row-2 column=f:name, timestamp=1589252853542, value=Zhou
row-3 column=f:name, timestamp=1589252853542, value=Li
row-4 column=f:namana, timestamp=1589252853542, value=xyz
4 row(s) in 0.0220 seconds
区别于上的是这里直接传入字符串进行比较,且只支持EQUAL和NOT_EQUAL两种比较符。
4. RegexStringComparator 构造过滤器
import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.RegexStringComparator
import org.apache.hadoop.hbase.filter.QualifierFilter
hbase(main):019:0> scan 'test',{FILTER => QualifierFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), RegexStringComparator.new('n[a-z]m'))}
ROW COLUMN+CELL
row-1 column=f:name, timestamp=1589252853542, value=Wang
row-2 column=f:name, timestamp=1589252853542, value=Zhou
row-3 column=f:name, timestamp=1589252853542, value=Li
row-4 column=f:namana, timestamp=1589252853542, value=xyz
4 row(s) in 0.0250 seconds
该比较器直接传入字符串进行比较,且只支持EQUAL和NOT_EQUAL两种比较符。若想使用第一种方式可以传入regexstring试一下,我的版本有点低暂时不支持,不再演示了。
注意这里的正则匹配指包含关系,对应底层find()方法。
QualifierFilter 不支持使用LongComparator比较器,且BitComparator、NullComparator 比较器用之甚少,也不再介绍。
查看文章全部源代码请访以下GitHub地址:
https://github.com/zhoupengbo/demos-bigdata/blob/master/hbase/hbase-filters-demos/src/main/java/com/zpb/demos/QualifierFilterDemo.java
转载请注明出处!欢迎关注本人微信公众号【HBase工作笔记】
HBase Filter 过滤器之QualifierFilter详解的更多相关文章
- HBase Filter 过滤器之RowFilter详解
前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考.RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase ...
- HBase Filter 过滤器之FamilyFilter详解
前言:本文详细介绍了 HBase FamilyFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.FamilyFilter 基于列族进行过滤,在工作中涉及 ...
- HBase Filter 过滤器之 ValueFilter 详解
前言:本文详细介绍了 HBase ValueFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.ValueFilter 基于列值进行过滤,在工作中涉及到需 ...
- HBase Filter 过滤器之 Comparator 原理及源码学习
前言:上篇文章HBase Filter 过滤器概述对HBase过滤器的组成及其家谱进行简单介绍,本篇文章主要对HBase过滤器之比较器作一个补充介绍,也算是HBase Filter学习的必备低阶魂技吧 ...
- Java 容器之Hashset 详解
Java 容器之Hashset 详解.http://blog.csdn.net/nvd11/article/details/27716511
- Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(上)
前言 Android中绘图离不开的就是Canvas了,Canvas是一个庞大的知识体系,有Java层的,也有jni层深入到Framework.Canvas有许多的知识内容,构建了一个武器库一般,所谓十 ...
- Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(下)
LinearGradient 线性渐变渲染器 LinearGradient中文翻译过来就是线性渐变的意思.线性渐变通俗来讲就是给起点设置一个颜色值如#faf84d,终点设置一个颜色值如#CC423C, ...
- hbase实践之数据读取详解
hbase基本存储组织结构与数据读取组织结构对比 Segment是Hbase2.0的概念,MemStore由一个可写的Segment,以及一个或多个不可写的Segments构成.故hbase 1.*版 ...
- 网页元素定位神器之Xpath详解
摘要: 经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或不太清楚,所以免不了每次总要查一些零碎的知识,感觉即很烦又浪费时间,所以对XPath归纳及总结一下. ... ...
随机推荐
- L3 多层感知机
**本小节用到的数据下载 1.涉及语句 import d2lzh1981 as d2l 数据1 : d2lzh1981 链接:https://pan.baidu.com/s/1LyaZ84Q4M75G ...
- Daily Scrum 1/12/2016
Zhaoyang & Yandong: Optimize the speech input interface Fuchen: Code refactor in the NLP module ...
- 9.回文数-LeetCode
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: 从左向右读, ...
- 如何将SqlServer配置为django的数据源(2.2以后版本)
django-pyodbc-azure 是一个官方推荐的 第三方django数据库支持backend. 根据官网的介绍django-pyodbc-azure 只能支持到 django 2.1.如果涉及 ...
- Caused by: java.io.IOException: Type mismath in vlaue from map: excepted org.apache.hadoop.io.InaWritable,received SC
解决办法: 看map和reduce的输入是不是对应,看看map和reduce设置的参数和下面的是否一致
- ajax 报0错误
本以为在本地测试不会跨越,结果检查下来就是跨域的问题!!!! 来源:https://www.cnblogs.com/xiezn/p/5651093.html PHP Ajax 跨域问题最佳解决方案 ...
- (二)PL/SQL特殊符号
PL/SQL标识符 PL/SQL标识符是常量,变量,异常,过程,游标和保留字.标识符是由一个字母后面可以跟更多的字母,数字,美元符号,下划线和数字符号,并且不得超过30个字符. 默认情况下,标识符不区 ...
- fseek 在以字符串模式打开的文件中工作不正常 [MSDN]
For streams opened in text mode, fseek and _fseeki64 have limited use, because carriage return-linef ...
- Springboot以Tomcat为容器实现http重定向到https的两种方式
1 简介 本文将介绍在Springboot中如何通过代码实现Http到Https的重定向,本文仅讲解Tomcat作为容器的情况,其它容器将在以后一一道来. 建议阅读之前的相关文章: (1) Sprin ...
- 【Linux常见命令】tar命令
[独立命令,只能用其中一个,和别的命令连用]这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个. -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末 ...