1. 程式人生 > >Hbase過濾器(四):FilterList API

Hbase過濾器(四):FilterList API

FilterList:

使用者可以提供多個過濾器共同限制返回到客戶端的結果,與其他單一功能的過濾器一樣,FilterList類實現了Filter介面,所以它可以通過組合多個過濾器的功能來實現某種效果,從而代替提供這類效果的過濾器。

方法類:

FilterList(List<Filter> rowFilters)

FilterList(Operator operator)

FilterList(Operator operator,List<Filter> rowFilters)

 

程式碼展示:

package filterList;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class filterList {
    private Configuration configuration = null;
    private Connection connection = null;
    @Test
    public void filterList() throws IOException {
        System.out.println("begin");
        configuration = HBaseConfiguration.create();
        connection = ConnectionFactory.createConnection(configuration);
        Table table = connection.getTable(TableName.valueOf("ns1:t1"));
        List<Filter> filters = new ArrayList<Filter>();
        Filter filter1 = new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,new BinaryComparator(Bytes.toBytes("row2")));
        filters.add(filter1);
        Filter filter2 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes("row3")));
        filters.add(filter2);
        Filter filter3 = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("name")));
        filters.add(filter3);
        FilterList filterList = new FilterList(filters);
        Scan scan = new Scan();
        scan.setFilter(filterList);
        ResultScanner resultScanner = table.getScanner(scan);
        for(Result result:resultScanner){
            for (KeyValue kv : result.raw()) {
                System.out.println("KV: "+kv+",value: "+Bytes.toString(kv.getValue()));
            }
        }
        resultScanner.close();

        System.out.println("-------------------------------------------------");
        FilterList filterList1 = new FilterList(FilterList.Operator.MUST_PASS_ONE,filters);
        Scan scan1 = new Scan();
        scan1.setFilter(filterList1);
        ResultScanner resultScanner1 = table.getScanner(scan1);
        for(Result result1:resultScanner1){
            for (KeyValue kv : result1.raw()) {
                System.out.println("KV: "+kv+",value: "+Bytes.toString(kv.getValue()));
            }
        }
        resultScanner1.close();
        table.close();
        System.out.println("end");
    }
}

結果展示: