前言:本文詳細(xì)介紹了 HBase DependentColumnFilter 過(guò)濾器 Java&Shell API 的使用,并貼出了相關(guān)示例代碼以供參考覆醇。DependentColumnFilter 也稱參考列過(guò)濾器永脓,是一種允許用戶指定一個(gè)參考列或引用列來(lái)過(guò)濾其他列的過(guò)濾器常摧,過(guò)濾的原則是基于參考列的時(shí)間戳來(lái)進(jìn)行篩選谎懦。
該過(guò)濾器嘗試找到該列所在的每一行界拦,并返回該行具有相同時(shí)間戳的全部鍵值對(duì);如果某行不包含這個(gè)指定的列享甸,則什么都不返回蛉威。參數(shù)dropDependentColumn 決定參考列被返回還是丟棄蚯嫌,為true時(shí)表示參考列被返回齐帚,為false時(shí)表示被丟棄对妄〖袅猓可以把DependentColumnFilter理解為一個(gè)valueFilter和一個(gè)時(shí)間戳過(guò)濾器的組合孝常。如果想要獲取同一時(shí)間線的數(shù)據(jù)可以考慮使用此過(guò)濾器蚓哩。比較器細(xì)節(jié)及原理請(qǐng)參照之前的更文:HBase Filter 過(guò)濾器之比較器 Comparator 原理及源碼學(xué)習(xí)岸梨。
一半开。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);
// 造數(shù)據(jù)
myBase.putRows(connection, tableName, data1); // 第一批數(shù)據(jù)
Thread.sleep(10);
myBase.putRows(connection, tableName, data2); // 第二批數(shù)據(jù)
}
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
中部代碼
向右滑動(dòng)滾動(dòng)條可查看輸出結(jié)果寂拆。
// 構(gòu)造方法一
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]
// 構(gòu)造方法二 boolean dropDependentColumn=true
DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), true); // [row-1:f2:c2:abc, row-2:f2:c2:1234abc567]
// 構(gòu)造方法二 boolean dropDependentColumn=false 默認(rèn)為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]
// 構(gòu)造方法三 + BinaryComparator 比較器過(guò)濾數(shù)據(jù)
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]
// 構(gòu)造方法三 + BinaryPrefixComparator 比較器過(guò)濾數(shù)據(jù)
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]
// 構(gòu)造方法三 + SubstringComparator 比較器過(guò)濾數(shù)據(jù)
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]
// 構(gòu)造方法三 + RegexStringComparator 比較器過(guò)濾數(shù)據(jù)
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]
// 構(gòu)造方法三 + RegexStringComparator 比較器過(guò)濾數(shù)據(jù)
DependentColumnFilter filter = new DependentColumnFilter(Bytes.toBytes("f1"), Bytes.toBytes("c1"), false,
CompareFilter.CompareOp.EQUAL, new RegexStringComparator("1234[a-z]")); // [] 思考題:與上例對(duì)比,想想為什么為空渺蒿?
該過(guò)濾器同時(shí)也支持各比較器的不同比較語(yǔ)法茂装,同之前介紹的各種過(guò)濾器是一樣的少态,這里不再一一舉例了彼妻。
尾部代碼
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 表數(shù)據(jù)一覽:
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. 簡(jiǎn)單構(gòu)造方法
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 構(gòu)造過(guò)濾器
方式一:
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
支持的比較運(yùn)算符:= != > >= < <=
牵舵,不再一一舉例畸颅。
方式二:
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
支持的比較運(yùn)算符:LESS涛癌、LESS_OR_EQUAL饼齿、EQUAL漾脂、NOT_EQUAL骨稿、GREATER坦冠、GREATER_OR_EQUAL
,不再一一舉例激涤。
推薦使用方式一判呕,更簡(jiǎn)潔方便侠草。
2. BinaryPrefixComparator 構(gòu)造過(guò)濾器
方式一:
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 構(gòu)造過(guò)濾器
方式一:
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
區(qū)別于上的是這里直接傳入字符串進(jìn)行比較,且只支持EQUAL
和NOT_EQUAL
兩種比較符童社。
4. RegexStringComparator 構(gòu)造過(guò)濾器
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
該比較器直接傳入字符串進(jìn)行比較,且只支持EQUAL
和NOT_EQUAL
兩種比較符灭抑。若想使用第一種方式可以傳入regexstring
試一下腾节,我的版本有點(diǎn)低暫時(shí)不支持案腺,不再演示了劈榨。
注意這里的正則匹配指包含關(guān)系同辣,對(duì)應(yīng)底層find()
方法。
DependentColumnFilter
不支持使用LongComparator
比較器响巢,且BitComparator
踪古、NullComparator
比較器用之甚少伏穆,也不再介紹蜈出。
到此為止铡原,所有的比較過(guò)濾器就總結(jié)完畢了商叹。
查看文章全部源代碼請(qǐng)?jiān)L以下GitHub地址:
https://github.com/zhoupengbo/demos-bigdata/blob/master/hbase/hbase-filters-demos/src/main/java/com/zpb/demos/DependentColumnFilterDemo.java