HBase Filter 过滤器之FamilyFilter详解
程序员文章站
2022-03-24 09:05:10
前言: 本文详细介绍了 HBase FamilyFilter 过滤器 Java&Shell API 的使用,并贴出了相关示例代码以供参考。FamilyFilter 基于列族进行过滤,在工作中涉及到需要通过HBase 列族进行数据过滤时可以考虑使用它。比较器细节及原理请参照之前的更文: "HBase ......
前言:本文详细介绍了 hbase familyfilter 过滤器 java&shell api 的使用,并贴出了相关示例代码以供参考。familyfilter 基于列族进行过滤,在工作中涉及到需要通过hbase 列族进行数据过滤时可以考虑使用它。
一。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
原文地址:https://www.cnblogs.com/zpb2016/p/12865244.html