Hbase filter

第一个例子是RowFilter的

Scan scan = new Scan();
scan.addColumn(Bytes.toBytes("colfam1"),
Bytes.toBytes("col-0"));
Filter filter1 = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
                                               new BinaryComparator(Bytes.toBytes("row-22")));
scan.setFilter(filter1);
ResultScanner scanner1 = table.getScanner(scan);
for (Result res : scanner1) {
    System.out.println(res);
}
scanner1.close();

Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,
                                              new RegexStringComparator(".*-.5"));
scan.setFilter(filter2);
ResultScanner scanner2 = table.getScanner(scan);
for (Result res : scanner2) {
     System.out.println(res);
}
scanner2.close();

Filter filter3 = new RowFilter(CompareFilter.CompareOp.EQUAL,
                                               new SubstringComparator("-5"));
scan.setFilter(filter3);
ResultScanner scanner3 = table.getScanner(scan);
for (Result res : scanner3) {
      System.out.println(res);
}
scanner3.close();

第二个例子是QualifierFilter的

Filter filter = new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
                                                  new BinaryComparator(Bytes.toBytes("col-2")));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
     System.out.println(result);
}
scanner.close();
Get get = new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result = table.get(get);

第三个例子是ValueFilter

Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL,
                                                new SubstringComparator(".4"));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
       for (KeyValue kv : result.raw()) {
             System.out.println("KV: " + kv + ", Value: " +
             Bytes.toString(kv.getValue()));
       }
}
scanner.close();
Get get = new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result = table.get(get);
for (KeyValue kv : result.raw()) {
     System.out.println("KV: " + kv + ", Value: " +
     Bytes.toString(kv.getValue()));
}

第四个例子是PageFilter,分页的filter

Filter filter = new PageFilter(15);
int totalRows = 0;
byte[] lastRow = null;
while (true) {
    Scan scan = new Scan();
    scan.setFilter(filter);
    if (lastRow != null) {
         byte[] startRow = Bytes.add(lastRow, POSTFIX);
        System.out.println("start row: " +Bytes.toStringBinary(startRow));
         scan.setStartRow(startRow);
    }
    ResultScanner scanner = table.getScanner(scan);  
    int localRows = 0;
    Result result;
    while ((result = scanner.next()) != null) {
        System.out.println(localRows++ + ": " + result);
         totalRows++;
         lastRow = result.getRow();
   }
   scanner.close();
   if (localRows == 0) break;
}
System.out.println("total rows: " + totalRows);

第五个例子是InclusiveStopFilter,它是什么意思呢,就是扫描到某个rowkey就停止,下面的例子是从row-3开始扫描,一直扫描到row-5结束。

Filter filter = new
InclusiveStopFilter(Bytes.toBytes("row-5"));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("row-3"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
    System.out.println(result);
}
scanner.close();

第六个SingleColumnValueFilter,这个是我们最常用的filter,我觉得很好用。

SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("colfam1"),
Bytes.toBytes("col-5"), CompareFilter.CompareOp.NOT_EQUAL,
new SubstringComparator("val-5"));
filter.setFilterIfMissing(true);
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
  for (KeyValue kv : result.raw()) {
     System.out.println("KV: " + kv + ", Value: " +
     Bytes.toString(kv.getValue()));
  }
}
scanner.close();
Get get = new Get(Bytes.toBytes("row-6"));
get.setFilter(filter);
Result result = table.get(get);
System.out.println("Result of get: ");
for (KeyValue kv : result.raw()) {
     System.out.println("KV: " + kv + ", Value: " +
     Bytes.toString(kv.getValue()));
}

第七个FilterList,它有两个选项,需要全部通过,还是一个通过就行,MUST_PASS_ALL,MUST_PASS_ONE。

List<Filter> filters = new ArrayList<Filter>();
Filter filter1 = new
RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row-03")));
filters.add(filter1);
Filter filter2 = new
RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row-06")));
filters.add(filter2);
Filter filter3 = new
QualifierFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("col-0[03]"));
filters.add(filter3);
FilterList filterList1 = new FilterList(filters);
Scan scan = new Scan();
scan.setFilter(filterList1);
ResultScanner scanner1 = table.getScanner(scan);
for (Result result : scanner1) {
for (KeyValue kv : result.raw()) {
    System.out.println("KV: " + kv + ", Value: " +
    Bytes.toString(kv.getValue()));
    }
}
scanner1.close();
FilterList filterList2 = new FilterList(

FilterList.Operator.MUST_PASS_ONE, filters);
scan.setFilter(filterList2);
ResultScanner scanner2 = table.getScanner(scan);
 for (Result result : scanner2) {
     for (KeyValue kv : result.raw()) {
    System.out.println("KV: " + kv + ", Value: " +
    Bytes.toString(kv.getValue()));
    }
}
scanner2.close();

这里要讲的是自定义filter,从FilterBase继承

public class CustomFilter extends FilterBase {
    private byte[] value = null;
    private boolean filterRow = true;

    public CustomFilter() {
        super();
    }

    public CustomFilter(byte[] value) {
        this.value = value;
    }

    @Override
    public void reset() {
        this.filterRow = true;
    }

    @Override
    public ReturnCode filterKeyValue(KeyValue kv) {
        if (Bytes.compareTo(value, kv.getValue()) == 0) {

            filterRow = false;
        }
        return ReturnCode.INCLUDE;
    }

    @Override
    public boolean filterRow() {
        return filterRow;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        Bytes.writeByteArray(dataOutput, this.value);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.value = Bytes.readByteArray(dataInput);
    }
}

然后打成jar包,要在hbase-env.sh中指明路径。

export  HBASE_CLASSPATH="/hbase/target/hbase-customfilter.jar",然后就可以在客户端中使用它了。

转自 http://www.cnblogs.com/cenyuhai/p/3231369.html

你可能感兴趣的:(filter,hbase)