hbase 自定义过滤器

//定义的过滤器(值过滤) 的类
// 写完定义过滤器的类  打成 jar  报 ,分发到 每个region  服务器中 
//分发完成后  需修改 hbase-env.sh  文件
//  export HBASE_CLASSPATH="jar  包路径 ,重新启动hbase";

public class TestFilter  extends FilterBase{
 private byte[] value=null;
 //判断每一行数据是否过滤
 private boolean filterbz=true;
 public TestFilter(){
  super();
 }
   public TestFilter(byte[] value){
    this.value=value;
   }
 public void write(DataOutput out) throws IOException {
  // TODO Auto-generated method stub
 Bytes.writeByteArray(out, value);
 }
 @Override
 public void reset(){
  this.filterbz=true;
 }
 public ReturnCode filterKeyValue(KeyValue kv){
  if(Bytes.compareTo(value, kv.getValue())==0){
   filterbz=false;
  }
  return ReturnCode.INCLUDE;
 }
  @Override
  public boolean filterRow(){
   return filterbz;
  }
 public void readFields(DataInput in) throws IOException {
  // TODO Auto-generated method stub
  this.value=Bytes.readByteArray(in);
 }

}

    //调用定义的过滤器
    public static void testglq(String tablename) throws IOException{
        HTable table = new HTable(getconfig(), tablename);
        Filter filter=new TestFilter(Bytes.toBytes("value1"));
     Scan scan=new Scan();
     scan.setFilter(filter);
        ResultScanner scanner=table.getScanner(scan);
     for (Result result : scanner) {
   System.out.println(result);
  }

    }

相关推荐