hbase的过滤器查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hbase的过滤器查询相关的知识,希望对你有一定的参考价值。
hbase的过滤器有很多:大致分为两大类:比较过滤器和专用过滤器,过滤器的作用是在服务端判断数据是否满足条件,然后只将满足条件的数据返回给客户端;
hbase 过滤器的比较运算符:
LESS ----- <
LESS_OR_EQUAL ----- <=
EQUAL ----- =
NOT_EQUAL ----- <>
GREATER_OR_EQUAL ----- >=
GREATER ----- >
NO_OP #排除所有
HBase 过滤器的比较器(指定比较机制):
BinaryComparator 按字节索引顺序比较指定字节数组,采用 Bytes.compareTo(byte[])
BinaryPrefixComparator 跟前面相同,只是比较左端的数据是否相同
NullComparator 判断给定的是否为空
BitComparator 按位比较
RegexStringComparator 提供一个正则的比较器,仅支持 EQUAL 和非 EQUAL
SubstringComparator 判断提供的子串是否出现在 value 中。
1. 比较过滤器
//行键过滤器 RowFilter
Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL, new
BinaryComparator(Bytes.toBytes("user0000")));
scan.setFilter(filter1);
//列簇过滤器 FamilyFilter
Filter filter1 = new FamilyFilter(CompareOp.LESS, new
BinaryComparator(Bytes.toBytes("base_info")));
scan.setFilter(filter1);
//列过滤器 QualifierFilter
Filter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, new
BinaryComparator(Bytes.toBytes("name")));
scan.setFilter(filter1);
//值过滤器 ValueFilter
Filter filter = new ValueFilter(CompareOp.EQUAL, new SubstringComparator("zhangsan") );
scan.setFilter(filter1);
//时间戳过滤器 TimestampsFilter
List<Long> tss = new ArrayList<Long>();
tss.add(1495398833002l);
Filter filter1 = new TimestampsFilter(tss);
scan.setFilter(filter1);
2.专用过滤器
//单列值过滤器 SingleColumnValueFilter ----会返回满足条件的整行
SingleColumnValueFilter filter = new SingleColumnValueFilter(
Bytes.toBytes("colfam1"),
Bytes.toBytes("col-5"),
CompareFilter.CompareOp.NOT_EQUAL,
new SubstringComparator("val-5"));
filter.setFilterIfMissing(true); //如果不设置为 true,则那些不包含指定 column 的行也会返回
scan.setFilter(filter1);
//单列值排除器 SingleColumnValueExcludeFilter -----返回排除了该列的结果
SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter(
Bytes.toBytes("colfam1"),
Bytes.toBytes("col-5"),
CompareFilter.CompareOp.NOT_EQUAL,
new SubstringComparator("val-5"));
filter.setFilterIfMissing(true); //如果不设置为 true,则那些不包含指定 column 的行也会返回
scan.setFilter(filter1);
//前缀过滤器 PrefixFilter----针对行键
Filter filter = new PrefixFilter(Bytes.toBytes("row1"));
scan.setFilter(filter1);
//列前缀过滤器 ColumnPrefixFilter
Filter filter = new ColumnPrefixFilter(Bytes.toBytes("qual2"));
scan.setFilter(filter1);
实战案例:
public class HBase_Filter01 {
private static String ZK_KEY = "hbase.zookeeper.quorum";
private static String ZK_VALUE = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
private static Configuration conf;
private static Connection connection;
private static Admin admin;
static {
conf = HBaseConfiguration.create();
conf.set(ZK_KEY,ZK_VALUE);
try {
connection= ConnectionFactory.createConnection(conf);
admin=connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Scan scan=new Scan();
ValueFilter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("a"));
scan.setFilter(filter);
TableName tableName =TableName.valueOf("user_info");
try {
Table table = connection.getTable(tableName);
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator=scanner.iterator();
while(iterator.hasNext()){
Result result = iterator.next();
System.out.println(result.list());
byte[] row = result.getRow();
System.out.println(new String(row));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上是关于hbase的过滤器查询的主要内容,如果未能解决你的问题,请参考以下文章