一、介绍
本节介绍HBase提供的最后两种过滤器,并且也介绍多个过滤器配合使用的方法。
二、详解
1、附加过滤器
(1)跳转过滤器:SkipFilter(Filter filter)
该过滤器的参数为一个过滤器。该过滤器的作用为:当参数中的过滤器过滤一个某一个KeyValue对象时,则跳转过滤器会将整行的数据进行过滤。
-
public void example(String tableName)
-
{
-
Configuration conf=init();
-
try {
-
HTable table= new HTable(conf, tableName);
-
//创建过滤器
-
ValueFilter filter= new ValueFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes( "row-10")));
-
SkipFilter skipFilter= new SkipFilter(filter);
-
Scan scan= new Scan();
-
scan.setFilter(filter);
-
//
-
ResultScanner rs=table.getScanner(scan);
-
Result result= null;
-
while((result=rs.next())!= null)
-
{
-
KeyValue[] kvs=result.raw();
-
for(KeyValue kv:kvs)
-
{
-
System.out.println(kv.toString());
-
}
-
}
-
//释放资源
-
rs.close();
-
table.close();
-
} catch (Exception e) {
-
// TODO: handle exception
-
}
-
}
(2)全匹配过滤器:WhileMatchFilter(Filter filter)
该过滤器需要填入一个过滤器作为参数。
该过滤器的作用于跳转过滤器相似,不过该过滤器当遇到第一个过滤整行数据的时候则会停止扫描过程。这样即使后面有数据符合条件也不会被传送到客户端。
-
public void example(String tableName)
-
{
-
Configuration conf=init();
-
try {
-
HTable table= new HTable(conf, tableName);
-
//创建过滤器
-
ValueFilter filter= new ValueFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes( "row-10")));
-
WhileMatchFilter filter= new WhileMatchFilter(filter);
-
Scan scan= new Scan();
-
scan.setFilter(filter);
-
//
-
ResultScanner rs=table.getScanner(scan);
-
Result result= null;
-
while((result=rs.next())!= null)
-
{
-
KeyValue[] kvs=result.raw();
-
for(KeyValue kv:kvs)
-
{
-
System.out.println(kv.toString());
-
}
-
}
-
//释放资源
-
rs.close();
-
table.close();
-
} catch (Exception e) {
-
// TODO: handle exception
-
}
-
}
2、FilterList:过滤器容器
FilterList的作用就是为了多个filter配合使用,因为在scan中,只能设置一个filter,因此在需要使用多个filter对象时,就需要使用FilterList容器将所有的filter装入,然后传入scan对象中。在Filterlist对象中,提供了多个filter的过滤关系:
(1)MUST_PASS_ALL :一行数据必须通过所有的过滤器才能被返回客户端
(2)MUST_PASS_ONE:一行数据中只要通过一个过滤器即可返回给客户端
在初始化FilterList对象时,可以进行模式的设置,FilterList对象的构造函数有三个:
FilterList(List<Filter> rowFilters)
FilterList(Operator operator)
FilterList(Operator operator,List<Filter> rowFilters)
-
public void mutilRowFilter(String tableName,CompareOp[] compareOps,ByteArrayComparable[] comparables)
-
{
-
Configuration conf=init();
-
try {
-
//创建表连接
-
HTable table= new HTable(conf, tableName);
-
//创建scan
-
Scan scan= new Scan();
-
int length=compareOps.length;
-
FilterList filterList= new FilterList(FilterList.Operator.MUST_PASS_ALL);
-
for( int i= 0;i<length;i++)
-
{
-
RowFilter filter= new RowFilter(compareOps[i], comparables[i]);
-
filterList.addFilter(filter);
-
}
-
scan.setFilter(filterList);
-
//执行返回结果
-
ResultScanner rs=table.getScanner(scan);
-
Result result= null;
-
while((result=rs.next())!= null)
-
{
-
KeyValue[] kvs=result.raw();
-
for(KeyValue kv:kvs)
-
{
-
System.out.println(kv.toString());
-
}
-
}
-
-
rs.close();
-
table.close();
-
} catch (Exception e) {
-
// TODO: handle exception
-
e.printStackTrace();
-
}