前言:本文详细介绍了 HBase FamilyFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考。FamilyFilter 基于列族进行过滤,在工作中涉及到需要通过HBase 列族进行数据过滤时可以考虑使用它。比较器细节及原理请参照之前的更文:HBase Filter 过滤器之比较器 Comparator 原理及源码学习

一。Java Api

头部代码

public class FamilyFilterDemo  {

    private static boolean isok = false;
private static String tableName = "test";
private static String[] cfs = new String[]{"f1","f2"};
private static String[] data = new String[]{"row-1:f1:c1:v1", "row-2:f1:c2:v2", "row-3:f2:c3:v3", "row-4:f2:c4:v4"}; 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 构造过滤器

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-3, row-4]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("f1"))); // [row-3, row-4]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2, row-3, row-4]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("f2"))); // [row-1, row-2]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2]

2. BinaryPrefixComparator 构造过滤器

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-3, row-4]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("f1"))); // [row-3, row-4]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2, row-3, row-4]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new BinaryComparator(Bytes.toBytes("f2"))); // [row-1, row-2]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("f1"))); // [row-1, row-2]

3. SubstringComparator 构造过滤器

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("1")); // [row-1, row-2]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.NOT_EQUAL, new SubstringComparator("f")); // []

4. RegexStringComparator 构造过滤器

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.NOT_EQUAL, new RegexStringComparator("f")); // []
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("f")); // [row-1, row-2, row-3, row-4]
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("2")); // [row-3, row-4]

尾部代码

        scan.setFilter(familyFilter);
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):002:0> scan 'test',{FILTER=>"FamilyFilter(=,'binary:f1')"}
ROW COLUMN+CELL
row-1 column=f1:c1, timestamp=1588834369334, value=v1
row-2 column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.1000 seconds

支持的比较运算符:= != > >= < <=,不再一一举例。

方式二:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.BinaryComparator
import org.apache.hadoop.hbase.filter.FamilyFilter hbase(main):006:0> scan 'test',{FILTER => FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryComparator.new(Bytes.toBytes('f1')))}
ROW COLUMN+CELL
row-1 column=f1:c1, timestamp=1588834369334, value=v1
row-2 column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0350 seconds

支持的比较运算符:LESS、LESS_OR_EQUAL、EQUAL、NOT_EQUAL、GREATER、GREATER_OR_EQUAL,不再一一举例。

推荐使用方式一,更简洁方便。

2. BinaryPrefixComparator 构造过滤器

方式一:

hbase(main):007:0> scan 'test',{FILTER=>"FamilyFilter(=,'binaryprefix:f1')"}
ROW COLUMN+CELL
row-1 column=f1:c1, timestamp=1588834369334, value=v1
row-2 column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0600 seconds

方式二:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator
import org.apache.hadoop.hbase.filter.FamilyFilter hbase(main):011:0> scan 'test',{FILTER => FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), BinaryPrefixComparator.new(Bytes.toBytes('f1')))}
ROW COLUMN+CELL
row-1 column=f1:c1, timestamp=1588834369334, value=v1
row-2 column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0290 seconds

其它同上。

3. SubstringComparator 构造过滤器

方式一:

hbase(main):012:0> scan 'test',{FILTER=>"FamilyFilter(=,'substring:f1')"}
ROW COLUMN+CELL
row-1 column=f1:c1, timestamp=1588834369334, value=v1
row-2 column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0400 seconds

方式二:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.SubstringComparator
import org.apache.hadoop.hbase.filter.FamilyFilter hbase(main):016:0> scan 'test',{FILTER => FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), SubstringComparator.new('f1'))}
ROW COLUMN+CELL
row-1 column=f1:c1, timestamp=1588834369334, value=v1
row-2 column=f1:c2, timestamp=1588834369334, value=v2
2 row(s) in 0.0330 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.FamilyFilter hbase(main):018:0> scan 'test',{FILTER => FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'), RegexStringComparator.new('f'))}
ROW COLUMN+CELL
row-1 column=f1:c1, timestamp=1588834369334, value=v1
row-2 column=f1:c2, timestamp=1588834369334, value=v2
row-3 column=f2:c3, timestamp=1588834369334, value=v3
row-4 column=f2:c4, timestamp=1588834369334, value=v4
4 row(s) in 0.0600 seconds

该比较器直接传入字符串进行比较,且只支持EQUAL和NOT_EQUAL两种比较符。若想使用第一种方式可以传入regexstring试一下,我的版本有点低暂时不支持,不再演示了。

注意这里的正则匹配指包含关系,对应底层find()方法。

FamilyFilter 不支持使用LongComparator比较器,且BitComparator、NullComparator 比较器用之甚少,也不再介绍。

查看文章全部源代码请访以下GitHub地址:

https://github.com/zhoupengbo/demos-bigdata/blob/master/hbase/hbase-filters-demos/src/main/java/com/zpb/demos/FamilyFilterDemo.java

转载请注明出处!欢迎关注本人微信公众号【HBase工作笔记】

HBase Filter 过滤器之FamilyFilter详解的更多相关文章

  1. HBase Filter 过滤器之RowFilter详解

    前言:本文详细介绍了HBase RowFilter过滤器Java&Shell API的使用,并贴出了相关示例代码以供参考.RowFilter 基于行键进行过滤,在工作中涉及到需要通过HBase ...

  2. HBase Filter 过滤器之QualifierFilter详解

    前言:本文详细介绍了 HBase QualifierFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.QualifierFilter 基于列名进行过滤, ...

  3. HBase Filter 过滤器之 ValueFilter 详解

    前言:本文详细介绍了 HBase ValueFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考.ValueFilter 基于列值进行过滤,在工作中涉及到需 ...

  4. HBase Filter 过滤器之 Comparator 原理及源码学习

    前言:上篇文章HBase Filter 过滤器概述对HBase过滤器的组成及其家谱进行简单介绍,本篇文章主要对HBase过滤器之比较器作一个补充介绍,也算是HBase Filter学习的必备低阶魂技吧 ...

  5. Java 容器之Hashset 详解

    Java 容器之Hashset 详解.http://blog.csdn.net/nvd11/article/details/27716511

  6. Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(上)

    前言 Android中绘图离不开的就是Canvas了,Canvas是一个庞大的知识体系,有Java层的,也有jni层深入到Framework.Canvas有许多的知识内容,构建了一个武器库一般,所谓十 ...

  7. Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(下)

    LinearGradient 线性渐变渲染器 LinearGradient中文翻译过来就是线性渐变的意思.线性渐变通俗来讲就是给起点设置一个颜色值如#faf84d,终点设置一个颜色值如#CC423C, ...

  8. hbase实践之数据读取详解

    hbase基本存储组织结构与数据读取组织结构对比 Segment是Hbase2.0的概念,MemStore由一个可写的Segment,以及一个或多个不可写的Segments构成.故hbase 1.*版 ...

  9. 网页元素定位神器之Xpath详解

    摘要: 经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或不太清楚,所以免不了每次总要查一些零碎的知识,感觉即很烦又浪费时间,所以对XPath归纳及总结一下. ...     ...

随机推荐

  1. 小白们错过就没了!Python基础之注释&变量命名

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:DZQTesters PS:如有需要Python学习资料的小伙伴可以加 ...

  2. A - Oil Deposits DFS

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  3. vue2.x学习笔记(五)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12571062.html. 计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.如果在模板中放入太 ...

  4. sk-learn实现L2岭回归,对线性回归正则化

    岭回归算法: from sklearn.datasets import load_boston from sklearn.externals import joblib from sklearn.li ...

  5. ORM之单表、多表操作

    参考1 参考2 表与表之间的关系: 一对一(OneToOneField):一对一字段无论建在哪张关系表里面都可以,但是推荐建在查询频率比较高的那张表里面 一对多(ForeignKey):一对多字段建在 ...

  6. 0day笔记(1)PE文件格式与虚拟文件内存的映射

    PE文件格式 PE 文件格式把可执行文件分成若干个数据节(section),不同的资源被存放在不同的节中. 一个典型的 PE 文件中包含的节如下: .text 存放着二进制的机器代码 .data 初始 ...

  7. Selenium常见报错问题(2)- 解决和分析StaleElementReferenceException异常

    如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...

  8. 使用ExecutorService来停止线程服务

    文章目录 使用shutdown 使用shutdownNow 使用ExecutorService来停止线程服务 之前的文章中我们提到了ExecutorService可以使用shutdown和shutdo ...

  9. SQL之常用函数

    表8-2 中的SOUNDEX 需要做进一步的解释.SOUNDEX 是一个将任何文本串转换为描述其语音表示的字母数字模式的算法.SOUNDEX 考虑了类似的发音字符和音节,使得能对字符串进行发音比较而不 ...

  10. 【JAVA基础】10 Object类

    1. Object类概述 是类层次结构的根类 每个类都使用 Object 作为超类 所有类都直接或者间接的继承自该类 所有对象(包括数组)都实现这个类的方法. 2. Object的构造方法 publi ...