RowFilter是用來對rowkey進行過濾的,比較符以下:
java
Operator正則表達式 |
Descriptionapache |
LESSoop |
小於spa |
LESS_OR_EQUALcode |
小於等於orm |
EQUALip |
等於hadoop |
NOT_EQUALci |
不等於 |
GREATER_OR_EQUAL |
大於等於 |
GREATER |
大於 |
NO_OP |
排除全部 |
Comparator |
Description |
BinaryComparator |
使用Bytes.compareTo()比較 |
BinaryPrefixComparator |
和BinaryComparator差很少,從前面開始比較 |
NullComparator |
Does not compare against an actual value but whether a given one is null, or not null. |
BitComparator |
Performs a bitwise comparison, providing a BitwiseOp class with AND, OR, and XOR operators. |
RegexStringComparator |
正則表達式 |
SubstringComparator |
把數據當成字符串,用contains()來判斷 |
下面是相關比較符的例子
點擊(此處)摺疊或打開
package com.hxl; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.util.Bytes; import com.hxl.ConnectDB; public class RowFilterTest { public void rowfilter(String tablename, String rowkeyvalue) { Configuration configuration = null; configuration = new ConnectDB().getConf(); try { HTable table = new HTable(configuration, tablename); Filter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(rowkeyvalue))); // EQUAL = Scan s = new Scan(); s.setFilter(filter1); ResultScanner rs = table.getScanner(s); for (Result r : rs) { System.out.println("rowkey:" + new String(r.getRow())); for (KeyValue keyValue : r.raw()) { System.out.println("列族:" + new String(keyValue.getFamily()) + " 列:" + new String(keyValue.getQualifier()) + ":" + new String(keyValue.getValue())); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相關的過濾方法使用:
提取rowkey以01結尾數據
Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*01$"));
提取rowkey以包含201407的數據 Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator("201407")); 提取rowkey以123開頭的數據 Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryPrefixComparator("123".getBytes()));