HBase Filter 过滤器之RowFilter详解
前言:本文详细介绍了hbase rowfilter过滤器java&shell api的使用,并贴出了相关示例代码以供参考。rowfilter 基于行键进行过滤,在工作中涉及到需要通过hbase rowkey进行数据过滤时可以考虑使用它。比较器细节及原理请参照之前的更文:hbase filter 过滤器之比较器 comparator 原理及源码学习
一。java api
头部代码
public class rowfilterdemo { private static boolean isok = false; private static string tablename = "test"; private static string[] cfs = new string[]{"f"}; private static string[] data = new string[]{"row-ac:f:c1:v1", "row-ab:f:c2:v2", "row-bc:f:c3:v3", "row-abc:f: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 构造过滤器
rowfilter rowfilter = new rowfilter(comparefilter.compareop.equal, new binarycomparator(bytes.tobytes("row-ac"))); // [row-ac] rowfilter rowfilter = new rowfilter(comparefilter.compareop.not_equal, new binarycomparator(bytes.tobytes("row-ac"))); // [row-ab, row-abc, row-bc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.greater, new binarycomparator(bytes.tobytes("row-ac"))); // [row-bc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.greater_or_equal, new binarycomparator(bytes.tobytes("row-ac"))); // [row-ac, row-bc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.less, new binarycomparator(bytes.tobytes("row-ac"))); // [row-ab, row-abc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.less_or_equal, new binarycomparator(bytes.tobytes("row-ac"))); // [row-ab, row-abc, row-ac]
2. binaryprefixcomparator 构造过滤器
rowfilter rowfilter = new rowfilter(comparefilter.compareop.equal, new binaryprefixcomparator(bytes.tobytes("row-a"))); // [row-ab, row-abc, row-ac] rowfilter rowfilter = new rowfilter(comparefilter.compareop.not_equal, new binaryprefixcomparator(bytes.tobytes("row-a"))); // [row-bc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.greater, new binaryprefixcomparator(bytes.tobytes("row-a"))); // [row-bc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.greater_or_equal, new binaryprefixcomparator(bytes.tobytes("row-a"))); // [row-ab, row-abc, row-ac, row-bc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.less, new binaryprefixcomparator(bytes.tobytes("row-a"))); // [] rowfilter rowfilter = new rowfilter(comparefilter.compareop.less_or_equal, new binaryprefixcomparator(bytes.tobytes("row-a"))); // [row-ab, row-abc, row-ac]
3. substringcomparator 构造过滤器
rowfilter rowfilter = new rowfilter(comparefilter.compareop.equal, new substringcomparator("ab")); // [row-ab, row-abc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.not_equal, new substringcomparator("ab")); // [row-ac, row-bc]
4. regexstringcomparator 构造过滤器
rowfilter rowfilter = new rowfilter(comparefilter.compareop.not_equal, new regexstringcomparator("abc")); // [row-ab, row-ac, row-bc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.equal, new regexstringcomparator("abc")); // [row-abc] rowfilter rowfilter = new rowfilter(comparefilter.compareop.equal, new regexstringcomparator("a")); // [row-ab, row-abc, row-ac]
5. nullcomparator 构造过滤器
rowfilter rowfilter = new rowfilter(comparefilter.compareop.equal, new nullcomparator()); // [] rowfilter rowfilter = new rowfilter(comparefilter.compareop.not_equal, new nullcomparator()); // [row-ab, row-abc, row-ac, row-bc]
尾部代码
scan.setfilter(rowfilter); 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):006:0> scan 'test',{filter=>"rowfilter(=,'binary:row-ab')"} row column+cell row-ab column=f:c2, timestamp=1588156704669, value=v2 1 row(s) in 0.0140 seconds
支持的比较运算符:= != > >= < <=,不再一一举例。
方式二:
import org.apache.hadoop.hbase.filter.comparefilter import org.apache.hadoop.hbase.filter.binarycomparator import org.apache.hadoop.hbase.filter.rowfilter hbase(main):016:0> scan 'test',{filter => rowfilter.new(comparefilter::compareop.valueof('equal'), binarycomparator.new(bytes.tobytes('row-ab')))} row column+cell row-ab column=f:c2, timestamp=1588156704669, value=v2 1 row(s) in 0.0310 seconds
支持的比较运算符:less、less_or_equal、equal、not_equal、greater、greater_or_equal,不再一一举例。
推荐使用方式一,更简洁方便。
2. binaryprefixcomparator 构造过滤器
方式一:
hbase(main):023:0> scan 'test',{filter=>"rowfilter(=,'binaryprefix:row-ab')"} row column+cell row-ab column=f:c2, timestamp=1588156704669, value=v2 row-abc column=f:c4, timestamp=1588156704669, value=v4 2 row(s) in 0.0360 seconds
方式二:
import org.apache.hadoop.hbase.filter.comparefilter import org.apache.hadoop.hbase.filter.binaryprefixcomparator import org.apache.hadoop.hbase.filter.rowfilter hbase(main):027:0> scan 'test',{filter => rowfilter.new(comparefilter::compareop.valueof('equal'), binaryprefixcomparator.new(bytes.tobytes('row-ab')))} row column+cell row-ab column=f:c2, timestamp=1588156704669, value=v2 row-abc column=f:c4, timestamp=1588156704669, value=v4 2 row(s) in 0.0110 seconds
其它同上。
3. substringcomparator 构造过滤器
方式一:
hbase(main):001:0> scan 'test',{filter=>"rowfilter(=,'substring:row-ab')"} row column+cell row-ab column=f:c2, timestamp=1588156704669, value=v2 row-abc column=f:c4, timestamp=1588156704669, value=v4 2 row(s) in 0.3200 seconds
方式二:
import org.apache.hadoop.hbase.filter.comparefilter import org.apache.hadoop.hbase.filter.substringcomparator import org.apache.hadoop.hbase.filter.rowfilter hbase(main):007:0> scan 'test',{filter => rowfilter.new(comparefilter::compareop.valueof('equal'), substringcomparator.new('row-ab'))} row column+cell row-ab column=f:c2, timestamp=1588156704669, value=v2 row-abc column=f:c4, timestamp=1588156704669, value=v4 2 row(s) in 0.0230 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.rowfilter hbase(main):007:0> scan 'test',{filter => rowfilter.new(comparefilter::compareop.valueof('equal'), regexstringcomparator.new('row-ab'))} row column+cell row-ab column=f:c2, timestamp=1588156704669, value=v2 row-abc column=f:c4, timestamp=1588156704669, value=v4 2 row(s) in 0.0230 seconds
该比较器直接传入字符串进行比较,且只支持equal和not_equal两种比较符。若想使用第一种方式可以传入regexstring试一下,我的版本有点低暂时不支持,不再演示了。
注意这里的正则匹配指包含关系,对应底层find()方法。
此外,rowfilter 不支持使用longcomparator比较器,且bitcomparator、nullcomparator 比较器用之甚少,也不再介绍。
查看文章全部源代码请访以下github地址:
https://github.com/zhoupengbo/demos-bigdata/blob/master/hbase/hbase-filters-demos/src/main/java/com/zpb/demos/rowfilterdemo.java
转载请注明出处!欢迎关注本人微信公众号【hbase工作笔记】
上一篇: JAVA并发编程 如何解决原子性问题
下一篇: Redis教程(八):事务详解