HBase Filter 過濾器之 DependentColumnFilter 詳解

前言:本文詳細介紹了 HBase DependentColumnFilter 過濾器 Java&Shell API 的使用,並貼出了相關示例代碼以供參考。DependentColumnFilter 也稱參考列過濾器,是一種允許用戶指定一個參考列或引用列來過濾其他列的過濾器,過濾的原則是基於參考列的時間戳來進行篩選。

該過濾器嘗試找到該列所在的每一行,並返回該行具有相同時間戳的全部鍵值對;如果某行不包含這個指定的列,則什麼都不返回。參數dropDependentColumn 決定參考列被返回還是丟棄,爲true時表示參考列被返回,爲false時表示被丟棄。可以把DependentColumnFilter理解爲一個valueFilter和一個時間戳過濾器的組合。如果想要獲取同一時間線的數據可以考慮使用此過濾器。比較器細節及原理請參照之前的更文:HBase Filter 過濾器之比較器 Comparator 原理及源碼學習

一。Java Api

頭部代碼

public class DependentColumnFilterDemo {

    private static boolean isok = false;
    private static String tableName = "test";
    private static String[] cfs = new String[]{"f1", "f2"};
    private static String[] data1 = new String[]{"row-1:f2:c3:1234abc56", "row-3:f1:c3:1234321"};
    private static String[] data2 = new String[]{
            "row-1:f1:c1:abcdefg", "row-1:f2:c2:abc", "row-2:f1:c1:abc123456", "row-2:f2:c2:1234abc567"
    };

    public static void main(String[] args) throws IOException, InterruptedException {

        MyBase myBase = new MyBase();
        Connection connection = myBase.createConnection();
        if (isok) {
            myBase.deleteTable(connection, tableName);
            myBase.createTable(connection, tableName, cfs);
            // 造數據
            myBase.putRows(connection, tableName, data1);  // 第一批數據
            Thread.sleep(10);
            myBase.putRows(connection, tableName, data2);  // 第二批數據
        }
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();

中部代碼
向右滑動滾動條可查看輸出結果。

        // 構造方法一
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"));  // [row-1:f1:c1:abcdefg, row-1:f2:c2:abc, row-2:f1:c1:abc123456, row-2:f2:c2:1234abc567]

        // 構造方法二 boolean dropDependentColumn=true
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), true);  // [row-1:f2:c2:abc, row-2:f2:c2:1234abc567]

        // 構造方法二 boolean dropDependentColumn=false  默認爲false
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false); // [row-1:f1:c1:abcdefg, row-1:f2:c2:abc, row-2:f1:c1:abc123456, row-2:f2:c2:1234abc567]

        // 構造方法三 + BinaryComparator 比較器過濾數據
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,
                CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("abcdefg"))); // [row-1:f1:c1:abcdefg, row-1:f2:c2:abc]

        // 構造方法三 + BinaryPrefixComparator 比較器過濾數據
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,
                CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("abc")));  // [row-1:f1:c1:abcdefg, row-1:f2:c2:abc, row-2:f1:c1:abc123456, row-2:f2:c2:1234abc567]

        // 構造方法三 + SubstringComparator 比較器過濾數據
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,
                CompareFilter.CompareOp.EQUAL, new SubstringComparator("1234"));  // [row-2:f1:c1:abc123456, row-2:f2:c2:1234abc567]

        // 構造方法三 + RegexStringComparator 比較器過濾數據
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,
                CompareFilter.CompareOp.EQUAL, new RegexStringComparator("[a-z]"));  // [row-1:f1:c1:abcdefg, row-1:f2:c2:abc, row-2:f1:c1:abc123456, row-2:f2:c2:1234abc567]

        // 構造方法三 + RegexStringComparator 比較器過濾數據
        DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,
                CompareFilter.CompareOp.EQUAL, new RegexStringComparator("1234[a-z]"));  // []  思考題:與上例對比,想想爲什麼爲空?

該過濾器同時也支持各比較器的不同比較語法,同之前介紹的各種過濾器是一樣的,這裏不再一一舉例了。

尾部代碼

		scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        LinkedList<String> keys = new LinkedList<>();
        while (iterator.hasNext()) {
            String key = "";
            Result result = iterator.next();
            for (Cell cell : result.rawCells()) {
                byte[] rowkey = CellUtil.cloneRow(cell);
                byte[] family = CellUtil.cloneFamily(cell);
                byte[] column = CellUtil.cloneQualifier(cell);
                byte[] value = CellUtil.cloneValue(cell);
                key = Bytes.toString(rowkey) + ":" + Bytes.toString(family) + ":" + Bytes.toString(column) + ":" + Bytes.toString(value);
                keys.add(key);
            }
        }
        System.out.println(keys);
        scanner.close();
        table.close();
        connection.close();
    }
}

二。Shell Api

HBase test 表數據一覽:

hbase(main):009:0> scan 'test'
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-1                                           column=f2:c3, timestamp=1589794115241, value=1234abc56
 row-2                                           column=f1:c1, timestamp=1589794115268, value=abc123456
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
 row-3                                           column=f1:c3, timestamp=1589794115241, value=1234321
3 row(s) in 0.0280 seconds

0. 簡單構造方法

hbase(main):006:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f1:c1, timestamp=1589794115268, value=abc123456
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0450 seconds

hbase(main):008:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1',false)"}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f1:c1, timestamp=1589794115268, value=abc123456
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0310 seconds

hbase(main):007:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1',true)"}
ROW                                              COLUMN+CELL
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0250 seconds

1. BinaryComparator 構造過濾器

方式一:

hbase(main):004:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1',false,=,'binary:abcdefg')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
1 row(s) in 0.0330 seconds

hbase(main):005:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1',true,=,'binary:abcdefg')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
1 row(s) in 0.0120 seconds

支持的比較運算符:= != > >= < <=,不再一一舉例。

方式二:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.BinaryComparator
import org.apache.hadoop.hbase.filter.DependentColumnFilter

hbase(main):016:0> scan 'test',{FILTER => DependentColumnFilter.new(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,CompareFilter::CompareOp.valueOf('EQUAL'), BinaryComparator.new(Bytes.toBytes('abcdefg')))}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
1 row(s) in 0.0170 seconds

hbase(main):017:0> scan 'test',{FILTER => DependentColumnFilter.new(Bytes.toBytes("f1"), Bytes.toBytes("c1"), true,CompareFilter::CompareOp.valueOf('EQUAL'), BinaryComparator.new(Bytes.toBytes('abcdefg')))}
ROW                                              COLUMN+CELL
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
1 row(s) in 0.0140 seconds

支持的比較運算符:LESS、LESS_OR_EQUAL、EQUAL、NOT_EQUAL、GREATER、GREATER_OR_EQUAL,不再一一舉例。

推薦使用方式一,更簡潔方便。

2. BinaryPrefixComparator 構造過濾器

方式一:

hbase(main):019:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1',false,=,'binaryprefix:abc')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f1:c1, timestamp=1589794115268, value=abc123456
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0330 seconds

hbase(main):020:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1',true,=,'binaryprefix:abc')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
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.DependentColumnFilter

hbase(main):023:0> scan 'test',{FILTER => DependentColumnFilter.new(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,CompareFilter::CompareOp.valueOf('EQUAL'), BinaryPrefixComparator.new(Bytes.toBytes('abc')))}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f1:c1, timestamp=1589794115268, value=abc123456
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0180 seconds

hbase(main):022:0> scan 'test',{FILTER => DependentColumnFilter.new(Bytes.toBytes("f1"), Bytes.toBytes("c1"), true,CompareFilter::CompareOp.valueOf('EQUAL'), BinaryPrefixComparator.new(Bytes.toBytes('abc')))}
ROW                                              COLUMN+CELL
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0190 seconds

其它同上。

3. SubstringComparator 構造過濾器

方式一:

hbase(main):025:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1',false,=,'substring:abc')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f1:c1, timestamp=1589794115268, value=abc123456
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0340 seconds

hbase(main):024:0> scan 'test',{FILTER=>"DependentColumnFilter('f1','c1',true,=,'substring:abc')"}
ROW                                              COLUMN+CELL
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0160 seconds

方式二:

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.SubstringComparator
import org.apache.hadoop.hbase.filter.DependentColumnFilter

hbase(main):028:0> scan 'test',{FILTER => DependentColumnFilter.new(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,CompareFilter::CompareOp.valueOf('EQUAL'), SubstringComparator.new('abc'))}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f1:c1, timestamp=1589794115268, value=abc123456
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0150 seconds

hbase(main):029:0> scan 'test',{FILTER => DependentColumnFilter.new(Bytes.toBytes("f1"), Bytes.toBytes("c1"), true,CompareFilter::CompareOp.valueOf('EQUAL'), SubstringComparator.new('abc'))}
ROW                                              COLUMN+CELL
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0170 seconds

區別於上的是這裏直接傳入字符串進行比較,且只支持EQUALNOT_EQUAL兩種比較符。

4. RegexStringComparator 構造過濾器

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.RegexStringComparator
import org.apache.hadoop.hbase.filter.DependentColumnFilter

hbase(main):035:0> scan 'test',{FILTER => DependentColumnFilter.new(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,CompareFilter::CompareOp.valueOf('EQUAL'), RegexStringComparator.new('[a-z]'))}
ROW                                              COLUMN+CELL
 row-1                                           column=f1:c1, timestamp=1589794115268, value=abcdefg
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f1:c1, timestamp=1589794115268, value=abc123456
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0170 seconds

hbase(main):034:0* scan 'test',{FILTER => DependentColumnFilter.new(Bytes.toBytes("f1"), Bytes.toBytes("c1"), true,CompareFilter::CompareOp.valueOf('EQUAL'), RegexStringComparator.new('[a-z]'))}
ROW                                              COLUMN+CELL
 row-1                                           column=f2:c2, timestamp=1589794115268, value=abc
 row-2                                           column=f2:c2, timestamp=1589794115268, value=1234abc567
2 row(s) in 0.0150 seconds

該比較器直接傳入字符串進行比較,且只支持EQUALNOT_EQUAL兩種比較符。若想使用第一種方式可以傳入regexstring試一下,我的版本有點低暫時不支持,不再演示了。

注意這裏的正則匹配指包含關係,對應底層find()方法。

DependentColumnFilter不支持使用LongComparator比較器,且BitComparatorNullComparator比較器用之甚少,也不再介紹。

到此爲止,所有的比較過濾器就總結完畢了。

查看文章全部源代碼請訪以下GitHub地址:

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

掃描二維碼關注博主公衆號

轉載請註明出處!歡迎關注本人微信公衆號【HBase工作筆記】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章