1. 程式人生 > 其它 >hbase scan過濾器命令語法出錯_HBase原理HBase Filter 過濾器之 FamilyFilter 詳解

hbase scan過濾器命令語法出錯_HBase原理HBase Filter 過濾器之 FamilyFilter 詳解

技術標籤:hbase scan過濾器命令語法出錯

e640c5818bfc0bb7b60272bcbc6eacf1.gif

前言:本文詳細介紹了 HBase FamilyFilter 過濾器 Java&Shell API 的使用,並貼出了相關示例程式碼以供參考。FamilyFilter 基於列族進行過濾,在工作中涉及到需要通過HBase 列族進行資料過濾時可以考慮使用它。比較器細節及原理請參照之前的更文:HBase Filter 過濾器之比較器 Comparator 原理及原始碼學習。

一。Java Api

頭部程式碼

publicclassFamilyFilterDemo{

privatestaticbooleanisok=false;
privatestaticStringtableName="test";
privatestaticString[]cfs=newString[]{"f1","f2"};
privatestaticString[]data=newString[]{"row-1:f1:c1:v1","row-2:f1:c2:v2","row-3:f2:c3:v3","row-4:f2:c4:v4"};

publicstaticvoidmain(String[]args)throwsIOException{

MyBasemyBase=newMyBase();
Connectionconnection=myBase.createConnection();
if(isok){
myBase.deleteTable(connection,tableName);
myBase.createTable(connection,tableName,cfs);
myBase.putRows(connection,tableName,data);//造資料
}
Tabletable=connection.getTable(TableName.valueOf(tableName));
Scanscan=newScan();

中部程式碼
向右滑動滾動條可檢視輸出結果。

1. BinaryComparator 構造過濾器

FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.EQUAL,newBinaryComparator(Bytes.toBytes("f1")));//[row-1,row-2]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.NOT_EQUAL,newBinaryComparator(Bytes.toBytes("f1")));//[row-3,row-4]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.GREATER,newBinaryComparator(Bytes.toBytes("f1")));//[row-3,row-4]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,newBinaryComparator(Bytes.toBytes("f1")));//[row-1,row-2,row-3,row-4]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.LESS,newBinaryComparator(Bytes.toBytes("f2")));//[row-1,row-2]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,newBinaryComparator(Bytes.toBytes("f1")));//[row-1,row-2]

2. BinaryPrefixComparator 構造過濾器

FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.EQUAL,newBinaryComparator(Bytes.toBytes("f1")));//[row-1,row-2]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.NOT_EQUAL,newBinaryComparator(Bytes.toBytes("f1")));//[row-3,row-4]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.GREATER,newBinaryComparator(Bytes.toBytes("f1")));//[row-3,row-4]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,newBinaryComparator(Bytes.toBytes("f1")));//[row-1,row-2,row-3,row-4]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.LESS,newBinaryComparator(Bytes.toBytes("f2")));//[row-1,row-2]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,newBinaryComparator(Bytes.toBytes("f1")));//[row-1,row-2]

3. SubstringComparator 構造過濾器

FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.EQUAL,newSubstringComparator("1"));//[row-1,row-2]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.NOT_EQUAL,newSubstringComparator("f"));//[]

4. RegexStringComparator 構造過濾器

FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.NOT_EQUAL,newRegexStringComparator("f"));//[]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.EQUAL,newRegexStringComparator("f"));//[row-1,row-2,row-3,row-4]
FamilyFilterfamilyFilter=newFamilyFilter(CompareFilter.CompareOp.EQUAL,newRegexStringComparator("2"));//[row-3,row-4]

尾部程式碼

scan.setFilter(familyFilter);
ResultScannerscanner=table.getScanner(scan);
Iteratoriterator=scanner.iterator();
LinkedListrowkeys=newLinkedList<>();while(iterator.hasNext()){
Resultresult=iterator.next();
Stringrowkey=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')"}
ROWCOLUMN+CELL
row-1column=f1:c1,timestamp=1588834369334,value=v1
row-2column=f1:c2,timestamp=1588834369334,value=v2
2row(s)in0.1000seconds

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

方式二:

importorg.apache.hadoop.hbase.filter.CompareFilter
importorg.apache.hadoop.hbase.filter.BinaryComparator
importorg.apache.hadoop.hbase.filter.FamilyFilter

hbase(main):006:0>scan'test',{FILTER=>FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'),BinaryComparator.new(Bytes.toBytes('f1')))}
ROWCOLUMN+CELL
row-1column=f1:c1,timestamp=1588834369334,value=v1
row-2column=f1:c2,timestamp=1588834369334,value=v2
2row(s)in0.0350seconds

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

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

2. BinaryPrefixComparator 構造過濾器

方式一:

hbase(main):007:0>scan'test',{FILTER=>"FamilyFilter(=,'binaryprefix:f1')"}
ROWCOLUMN+CELL
row-1column=f1:c1,timestamp=1588834369334,value=v1
row-2column=f1:c2,timestamp=1588834369334,value=v2
2row(s)in0.0600seconds

方式二:

importorg.apache.hadoop.hbase.filter.CompareFilter
importorg.apache.hadoop.hbase.filter.BinaryPrefixComparator
importorg.apache.hadoop.hbase.filter.FamilyFilter

hbase(main):011:0>scan'test',{FILTER=>FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'),BinaryPrefixComparator.new(Bytes.toBytes('f1')))}
ROWCOLUMN+CELL
row-1column=f1:c1,timestamp=1588834369334,value=v1
row-2column=f1:c2,timestamp=1588834369334,value=v2
2row(s)in0.0290seconds

其它同上。

3. SubstringComparator 構造過濾器

方式一:

hbase(main):012:0>scan'test',{FILTER=>"FamilyFilter(=,'substring:f1')"}
ROWCOLUMN+CELL
row-1column=f1:c1,timestamp=1588834369334,value=v1
row-2column=f1:c2,timestamp=1588834369334,value=v2
2row(s)in0.0400seconds

方式二:

importorg.apache.hadoop.hbase.filter.CompareFilter
importorg.apache.hadoop.hbase.filter.SubstringComparator
importorg.apache.hadoop.hbase.filter.FamilyFilter

hbase(main):016:0>scan'test',{FILTER=>FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'),SubstringComparator.new('f1'))}
ROWCOLUMN+CELL
row-1column=f1:c1,timestamp=1588834369334,value=v1
row-2column=f1:c2,timestamp=1588834369334,value=v2
2row(s)in0.0330seconds

區別於上的是這裡直接傳入字串進行比較,且只支援EQUAL和NOT_EQUAL兩種比較符。

4. RegexStringComparator 構造過濾器

importorg.apache.hadoop.hbase.filter.CompareFilter
importorg.apache.hadoop.hbase.filter.RegexStringComparator
importorg.apache.hadoop.hbase.filter.FamilyFilter

hbase(main):018:0>scan'test',{FILTER=>FamilyFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'),RegexStringComparator.new('f'))}
ROWCOLUMN+CELL
row-1column=f1:c1,timestamp=1588834369334,value=v1
row-2column=f1:c2,timestamp=1588834369334,value=v2
row-3column=f2:c3,timestamp=1588834369334,value=v3
row-4column=f2:c4,timestamp=1588834369334,value=v4
4row(s)in0.0600seconds

該比較器直接傳入字串進行比較,且只支援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

HBase官方社群推薦必讀好文

HBase 測試|HBase 2.2.1隨機讀寫效能測試

HBase 原理|HBase 記憶體管理之 MemStore 進化論

HBase 實踐|說好不哭,但 HBase 2.0 真的好用到哭

HBase 抗戰總結|阿里巴巴 HBase 高可用8年抗戰回憶錄

關注 HBase技術社群,獲取更多技術乾貨

c7bf096c7127d6343d46bd9323f02596.png

我知道你會再看一下的