RowFilter是用来对rowkey进行过滤的,比较符如下:
Operator | Description | LESS | 小于 | LESS_OR_EQUAL | 小于等于 | EQUAL | 等于 | NOT_EQUAL | 不等于 | 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();
}
}
}
如果是大于替换为GREATER,至于正则,不会写了,楼主可以问问正则高手
Filter filter1 = new RowFilter(CompareFilter.CompareOp.GREATER,
new RegexStringComparator(Bytes.toBytes(rowkeyvalue)));
###########################################################
相关的过滤方法使用:
提取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()));
|