HBase Filter 过滤器之FamilyFilter详解

前言:本文详细介绍了 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 构造过滤器

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxzyg.html