前言: 本文詳細介紹了HBase RowFilter過濾器Java&Shell API的使用,並貼出了相關示例代碼以供參考。RowFilter 基於行鍵進行過濾,在工作中涉及到需要通過HBase Rowkey進行數據過濾時可以考慮使用它。比較器細節及原理請參照之前的更文: "HBase Filter ...
前言:本文詳細介紹了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工作筆記】