Hbase自定义过滤器

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterBase;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

//自定义过滤器  实际用的时候要进行序列化
public class ColumnCompareFilter extends FilterBase {

    private String columnAName;
    private String columnBName;
    private byte[] columnAValue;
    private byte[] columnBValue;

    public ColumnCompareFilter(String columnAName,String columnBName){
        this.columnAName = columnAName;
        this.columnBName = columnBName;
    }

    @Override
    public ReturnCode filterKeyValue(Cell cell) throws IOException {
        String[] columnAInfo = StringUtils.split(columnAName, ":");
        //判断cell的列簇和列和我们指定的是否相同
        if(CellUtil.matchingColumn(cell,Bytes.toBytes(columnAInfo[0]),Bytes.toBytes(columnAInfo[1]))){
            //获取值
            columnAValue = CellUtil.cloneValue(cell);
        }

        String[] columnBInfo = StringUtils.split(columnBName, ":");
        if(CellUtil.matchingColumn(cell,Bytes.toBytes(columnBInfo[0]),Bytes.toBytes(columnBInfo[1]))){
            columnBValue = CellUtil.cloneValue(cell);
        }
        //返回码,结果要包含keyvalue
        return ReturnCode.INCLUDE;
    }
    public boolean hasFilterRow() {
        return true;
    }

    //进行比较
    public boolean filterRow() throws IOException {
        int columnAValueInt = Bytes.toInt(columnAValue);
        int columnBValueInt = Bytes.toInt(columnBValue);
        return columnAValueInt<=columnBValueInt;
    }

    //每次调用新行的时候被调用
    public void reset() throws IOException {
        this.columnAValue = null;
        this.columnBValue =null;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36421826/article/details/80859336