http://www.cnblogs.com/skyl/p/4807793.html
比較運算符 CompareFilter.CompareOp
比較運算符用于定義比較關(guān)系,可以有以下幾類值供選擇:
- EQUAL 相等
- GREATER 大于
- GREATER_OR_EQUAL 大于等于
- LESS 小于
- LESS_OR_EQUAL 小于等于
- NOT_EQUAL 不等于
比較器 ByteArrayComparable
通過比較器可以實現(xiàn)多樣化目標(biāo)匹配效果,比較器有以下子類可以使用:
- BinaryComparator 匹配完整字節(jié)數(shù)組
- BinaryPrefixComparator 匹配字節(jié)數(shù)組前綴
- BitComparator 不常用
- NullComparator 不常用
- RegexStringComparator?匹配正則表達(dá)式
- SubstringComparator?匹配子字符串
1.多重過濾器--FilterList(Shell不支持)
FilterList代表一個過濾器鏈,它可以包含一組即將應(yīng)用于目標(biāo)數(shù)據(jù)集的過濾器,過濾器間具有“與”FilterList.Operator.MUST_PASS_ALL?和“或”?FilterList.Operator.MUST_PASS_ONE?關(guān)系。
//結(jié)合過濾器,獲取所有age在15到30之間的行
private static void scanFilter() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");// AndFilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);// >=15SingleColumnValueFilter filter1 = new SingleColumnValueFilter("info".getBytes(), "age".getBytes(), CompareOp.GREATER_OR_EQUAL, "15".getBytes());// =<30SingleColumnValueFilter filter2 = new SingleColumnValueFilter("info".getBytes(), "age".getBytes(), CompareOp.LESS_OR_EQUAL, "30".getBytes());filterList.addFilter(filter1);filterList.addFilter(filter2); Scan scan = new Scan();// set Filterscan.setFilter(filterList);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
2. 列值過濾器--SingleColumnValueFilter
用于測試列值相等(CompareOp.EQUAL ),不等(CompareOp.NOT_EQUAL),或單側(cè)范圍 (如CompareOp.GREATER)。構(gòu)造函數(shù):
2.1.比較的關(guān)鍵字是一個字符數(shù)組(Shell不支持?)
SingleColumnValueFilter(byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value)
//SingleColumnValueFilter例子
private static void scanFilter01() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");SingleColumnValueFilter scvf = new SingleColumnValueFilter("info".getBytes(), "age".getBytes(), CompareOp.EQUAL, "18".getBytes());Scan scan = new Scan();scan.setFilter(scvf);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
2.2.比較的關(guān)鍵字是一個比較器ByteArrayComparable
SingleColumnValueFilter(byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, ByteArrayComparable comparator)
//SingleColumnValueFilter例子2 -- RegexStringComparator
private static void scanFilter02() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");
//值比較的正則表達(dá)式 -- RegexStringComparator//匹配info:age值以"4"結(jié)尾RegexStringComparator comparator = new RegexStringComparator(".4");//第四個參數(shù)不一樣SingleColumnValueFilter scvf = new SingleColumnValueFilter("info".getBytes(), "age".getBytes(), CompareOp.EQUAL, comparator);Scan scan = new Scan();scan.setFilter(scvf);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
hbase(main):032:0> scan 'users',{FILTER=>"SingleColumnValueFilter('info','age',=,'regexstring:.4')"}
ROW COLUMN+CELL xiaoming01 column=address:contry, timestamp=1442000277200, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=address:country, timestamp=1442000228945, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=info:age, timestamp=1441998917568, value=24 xiaoming02 column=info:age, timestamp=1441998917594, value=24 xiaoming03 column=info:age, timestamp=1441998919607, value=24
3 row(s) in 0.0130 seconds
//SingleColumnValueFilter例子2 -- SubstringComparator
private static void scanFilter03() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//檢測一個子串是否存在于值中(大小寫不敏感) -- SubstringComparator//過濾age值中包含'4'的RowKeySubstringComparator comparator = new SubstringComparator("4");//第四個參數(shù)不一樣SingleColumnValueFilter scvf = new SingleColumnValueFilter("info".getBytes(), "age".getBytes(), CompareOp.EQUAL, comparator);Scan scan = new Scan();scan.setFilter(scvf);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
hbase(main):033:0> scan 'users',{FILTER=>"SingleColumnValueFilter('info','age',=,'substring:4')"}
ROW COLUMN+CELL xiaoming01 column=address:contry, timestamp=1442000277200, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=address:country, timestamp=1442000228945, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=info:age, timestamp=1441998917568, value=24 xiaoming02 column=info:age, timestamp=1441998917594, value=24 xiaoming03 column=info:age, timestamp=1441998919607, value=24
3 row(s) in 0.0180 seconds
3.列名過濾器
由于HBase采用鍵值對保存內(nèi)部數(shù)據(jù),列名過濾器過濾一行的列名(ColumnFamily:Qualifiers)是否存在 , 對應(yīng)前節(jié)所述列值的情況。
3.1.基于Columun Family列族過濾數(shù)據(jù)的FamilyFilter
FamilyFilter(CompareFilter.CompareOp familyCompareOp, ByteArrayComparable familyComparator)
注意:
1.如果希望查找的是一個已知的列族,則使用?scan.addFamily(family);?比使用過濾器效率更高.
2.由于目前HBase對多列族支持不完善,所以該過濾器目前用途不大.
//基于列族過濾數(shù)據(jù)的FamilyFilter
private static void scanFilter04() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//過濾 = 'address'的列族//FamilyFilter familyFilter = new FamilyFilter(CompareOp.EQUAL, new BinaryComparator("address".getBytes()));//過濾以'add'開頭的列族FamilyFilter familyFilter = new FamilyFilter(CompareOp.EQUAL, new BinaryPrefixComparator("add".getBytes()));Scan scan = new Scan();scan.setFilter(familyFilter);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
hbase(main):021:0> scan 'users',{FILTER=>"FamilyFilter(=,'binaryprefix:add')"}
ROW COLUMN+CELL xiaoming column=address:city, timestamp=1441997498965, value=hangzhou xiaoming column=address:contry, timestamp=1441997498911, value=china xiaoming column=address:province, timestamp=1441997498939, value=zhejiang xiaoming01 column=address:contry, timestamp=1442000277200, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=address:country, timestamp=1442000228945, value=\xE4\xB8\xAD\xE5\x9B\xBD zhangyifei column=address:city, timestamp=1441997499108, value=jieyang zhangyifei column=address:contry, timestamp=1441997499077, value=china zhangyifei column=address:province, timestamp=1441997499093, value=guangdong zhangyifei column=address:town, timestamp=1441997500711, value=xianqiao
3 row(s) in 0.0400 seconds
3.2.基于Qualifier列名過濾數(shù)據(jù)的QualifierFilter
QualifierFilter(CompareFilter.CompareOp op, ByteArrayComparable qualifierComparator)
說明:該過濾器應(yīng)該比FamilyFilter更常用!
//基于Qualifier(列名)過濾數(shù)據(jù)的QualifierFilter
private static void scanFilter05() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//過濾列名 = 'age'所有RowKey//QualifierFilter qualifierFilter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator("age".getBytes()));//過濾列名 以'age'開頭 所有RowKey(包含age)//QualifierFilter qualifierFilter = new QualifierFilter(CompareOp.EQUAL, new BinaryPrefixComparator("age".getBytes()));//過濾列名 包含'age' 所有RowKey(包含age)//QualifierFilter qualifierFilter = new QualifierFilter(CompareOp.EQUAL, new SubstringComparator("age"));//過濾列名 符合'.ge'正則表達(dá)式 所有RowKeyQualifierFilter qualifierFilter = new QualifierFilter(CompareOp.EQUAL, new RegexStringComparator(".ge"));Scan scan = new Scan();scan.setFilter(qualifierFilter);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
hbase(main):020:0> scan 'users',{FILTER=>"QualifierFilter(=,'regexstring:.ge')"}
ROW COLUMN+CELL xiaoming column=info:age, timestamp=1441997971945, value=38 xiaoming01 column=info:age, timestamp=1441998917568, value=24 xiaoming02 column=info:age, timestamp=1441998917594, value=24 xiaoming03 column=info:age, timestamp=1441998919607, value=24 zhangyifei column=info:age, timestamp=1442247255446, value=18
5 row(s) in 0.0460 seconds
3.3.基于列名前綴過濾數(shù)據(jù)的ColumnPrefixFilter(該功能用QualifierFilter也能實現(xiàn))
ColumnPrefixFilter(byte[] prefix)?
注意:一個列名是可以出現(xiàn)在多個列族中的,該過濾器將返回所有列族中匹配的列。
//ColumnPrefixFilter例子
private static void scanFilter06() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//匹配 以'ag'開頭的所有的列ColumnPrefixFilter columnPrefixFilter = new ColumnPrefixFilter("ag".getBytes());Scan scan = new Scan();scan.setFilter(columnPrefixFilter);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
hbase(main):018:0> scan 'users',{FILTER=>"ColumnPrefixFilter('ag')"}
ROW COLUMN+CELL xiaoming column=info:age, timestamp=1441997971945, value=38 xiaoming01 column=info:age, timestamp=1441998917568, value=24 xiaoming02 column=info:age, timestamp=1441998917594, value=24 xiaoming03 column=info:age, timestamp=1441998919607, value=24 zhangyifei column=info:age, timestamp=1442247255446, value=18
5 row(s) in 0.0280 seconds
3.4.基于多個列名前綴過濾數(shù)據(jù)的MultipleColumnPrefixFilter
MultipleColumnPrefixFilter 和 ColumnPrefixFilter 行為差不多,但可以指定多個前綴。
//MultipleColumnPrefixFilter例子
private static void scanFilter07() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//匹配 以'a'或者'c'開頭 所有的列{二維數(shù)組}byte[][] prefixes =new byte[][]{"a".getBytes(), "c".getBytes()}; MultipleColumnPrefixFilter multipleColumnPrefixFilter = new MultipleColumnPrefixFilter(prefixes );Scan scan = new Scan();scan.setFilter(multipleColumnPrefixFilter);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
hbase(main):017:0> scan 'users',{FILTER=>"MultipleColumnPrefixFilter('a','c')"}
ROW COLUMN+CELL xiaoming column=address:city, timestamp=1441997498965, value=hangzhou xiaoming column=address:contry, timestamp=1441997498911, value=china xiaoming column=info:age, timestamp=1441997971945, value=38 xiaoming column=info:company, timestamp=1441997498889, value=alibaba xiaoming01 column=address:contry, timestamp=1442000277200, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=address:country, timestamp=1442000228945, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=info:age, timestamp=1441998917568, value=24 xiaoming02 column=info:age, timestamp=1441998917594, value=24 xiaoming03 column=info:age, timestamp=1441998919607, value=24 zhangyifei column=address:city, timestamp=1441997499108, value=jieyang zhangyifei column=address:contry, timestamp=1441997499077, value=china zhangyifei column=info:age, timestamp=1442247255446, value=18 zhangyifei column=info:company, timestamp=1441997499039, value=alibaba
5 row(s) in 0.0430 seconds
3.5.基于列范圍(不是行范圍)過濾數(shù)據(jù)ColumnRangeFilter
可用于獲得一個范圍的列,例如,如果你的一行中有百萬個列,但是你只希望查看列名從bbbb到dddd的范圍該方法從 HBase 0.92 版本開始引入一個列名是可以出現(xiàn)在多個列族中的,該過濾器將返回所有列族中匹配的列構(gòu)造函數(shù):
ColumnRangeFilter(byte[] minColumn, boolean minColumnInclusive, byte[] maxColumn, boolean maxColumnInclusive)
參數(shù)解釋:
- minColumn?- 列范圍的最小值,如果為空,則沒有下限
- minColumnInclusive?- 列范圍是否包含minColumn
- maxColumn?- 列范圍最大值,如果為空,則沒有上限
- maxColumnInclusive?- 列范圍是否包含maxColumn
//ColumnRangeFilter例子
private static void scanFilter08() throws IOException,
UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//匹配 以'a'開頭到以'c'開頭(不包含c) 所有的列 ColumnRangeFilter columnRangeFilter = new ColumnRangeFilter("a".getBytes(), true, "c".getBytes(), false);Scan scan = new Scan();scan.setFilter(columnRangeFilter);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
hbase(main):016:0> scan 'users',{FILTER=>"ColumnRangeFilter('a',true,'c',false)"}
ROW COLUMN+CELL xiaoming column=info:age, timestamp=1441997971945, value=38 xiaoming column=info:birthday, timestamp=1441997498851, value=1987-06-17 xiaoming01 column=info:age, timestamp=1441998917568, value=24 xiaoming02 column=info:age, timestamp=1441998917594, value=24 xiaoming03 column=info:age, timestamp=1441998919607, value=24 zhangyifei column=info:age, timestamp=1442247255446, value=18 zhangyifei column=info:birthday, timestamp=1441997498990, value=1987-4-17
5 row(s) in 0.0340 seconds
4.RowKey
當(dāng)需要根據(jù)行鍵特征查找一個范圍的行數(shù)據(jù)時,使用Scan的startRow和stopRow會更高效,但是,startRow和stopRow只能匹配行鍵的開始字符,而不能匹配中間包含的字符。當(dāng)需要針對行鍵進(jìn)行更復(fù)雜的過濾時,可以使用RowFilter。
構(gòu)造函數(shù):RowFilter(CompareFilter.CompareOp rowCompareOp, ByteArrayComparable rowComparator)
//RowFilter例子
private static void scanFilter09() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//匹配 行鍵包含'01' 所有的行 RowFilter rowFilter = new RowFilter(CompareOp.EQUAL, new SubstringComparator("01"));Scan scan = new Scan();scan.setFilter(rowFilter);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
hbase(main):013:0> scan 'users',{FILTER=>"RowFilter(=,'substring:01')"}
ROW COLUMN+CELL xiaoming01 column=address:contry, timestamp=1442000277200, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=address:country, timestamp=1442000228945, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming01 column=info:age, timestamp=1441998917568, value=24
1 row(s) in 0.0190 seconds
5.PageFilter(Shell不支持?)
指定頁面行數(shù),返回對應(yīng)行數(shù)的結(jié)果集。
需要注意的是,該過濾器并不能保證返回的結(jié)果行數(shù)小于等于指定的頁面行數(shù),因為過濾器是分別作用到各個region server的,它只能保證當(dāng)前region返回的結(jié)果行數(shù)不超過指定頁面行數(shù)。
構(gòu)造函數(shù):PageFilter(long pageSize)
//PageFilter例子
private static void scanFilter10() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//從RowKey為 "xiaoming" 開始,取3行(包含xiaoming) PageFilter pageFilter = new PageFilter(3L);Scan scan = new Scan();scan.setStartRow("xiaoming".getBytes());scan.setFilter(pageFilter);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
注意:由于該過濾器并不能保證返回的結(jié)果行數(shù)小于等于指定的頁面行數(shù),所以更好的返回指定行數(shù)的辦法是ResultScanner.next(int nbRows),即:
//上面Demo的改動版
private static void scanFilter11() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//從RowKey為 "xiaoming" 開始,取3行(包含xiaoming) //PageFilter pageFilter = new PageFilter(3L);Scan scan = new Scan();scan.setStartRow("xiaoming".getBytes());//scan.setFilter(pageFilter);ResultScanner rs = ht.getScanner(scan);//指定返回3行數(shù)據(jù)for(Result result : rs.next(3)){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
6.SkipFilter(Shell不支持)
根據(jù)整行中的每個列來做過濾,只要存在一列不滿足條件,整行都被過濾掉。
構(gòu)造函數(shù):SkipFilter(Filter filter)
例如,如果一行中的所有列代表的是不同物品的重量,則真實場景下這些數(shù)值都必須大于零,我們希望將那些包含任意列值為0的行都過濾掉。在這個情況下,我們結(jié)合ValueFilter和SkipFilter共同實現(xiàn)該目的:
scan.setFilter(new SkipFilter(new ValueFilter(CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes(0))));
//SkipFilter例子
private static void scanFilter12() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//跳過列值中包含"24"的所有列SkipFilter skipFilter = new SkipFilter(new ValueFilter(CompareOp.NOT_EQUAL, new BinaryComparator("24".getBytes())));Scan scan = new Scan();scan.setFilter(skipFilter);ResultScanner rs = ht.getScanner(scan);for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());}}ht.close();
}
7.Utility--FirstKeyOnlyFilter
該過濾器僅僅返回每一行中第一個cell的值,可以用于高效的執(zhí)行行數(shù)統(tǒng)計操作。估計實戰(zhàn)意義不大。
構(gòu)造函數(shù):public FirstKeyOnlyFilter()
//FirstKeyOnlyFilter例子
private static void scanFilter12() throws IOException,UnsupportedEncodingException {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://ncst:9000/hbase");conf.set("hbase.zookeeper.quorum", "ncst");HTable ht = new HTable(conf, "users");//返回每一行中的第一個cell的值FirstKeyOnlyFilter firstKeyOnlyFilter = new FirstKeyOnlyFilter();Scan scan = new Scan();scan.setFilter(firstKeyOnlyFilter);ResultScanner rs = ht.getScanner(scan);int i = 0;for(Result result : rs){for(Cell cell : result.rawCells()){System.out.println(new String(CellUtil.cloneRow(cell))+"\t"+new String(CellUtil.cloneFamily(cell))+"\t"+new String(CellUtil.cloneQualifier(cell))+"\t"+new String(CellUtil.cloneValue(cell),"UTF-8")+"\t"+cell.getTimestamp());i++;}}//輸出總的行數(shù)System.out.println(i);ht.close();
}
hbase(main):009:0> scan 'users',{FILTER=>'FirstKeyOnlyFilter()'}
ROW COLUMN+CELL xiaoming column=address:city, timestamp=1441997498965, value=hangzhou xiaoming01 column=address:contry, timestamp=1442000277200, value=\xE4\xB8\xAD\xE5\x9B\xBD xiaoming02 column=info:age, timestamp=1441998917594, value=24 xiaoming03 column=info:age, timestamp=1441998919607, value=24 zhangyifei column=address:city, timestamp=1441997499108, value=jieyang
5 row(s) in 0.0240 seconds
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/8303056.html
總結(jié)
以上是生活随笔為你收集整理的HBase Filter及对应Shell--转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。