hbase2.1.x java SingleColumnValueFilter 列不存在时,无法过滤,满足所有过滤条件

描述问题

添加测试数据

清空表:

truncate 'book'

添加数据:

put 'book','1001','c1:title','htlm从入门到放弃'
put 'book','1001','c1:author','lucy'

put 'book','1002','c1:title','css从入门到放弃'
put 'book','1002','c1:author','lili'
put 'book','1002','c1:price','099.90'

put 'book','1003','c1:title','hadoop从入门到放弃'
put 'book','1003','c1:author','韩梅梅'
put 'book','1003','c1:price','190.00'

put 'book','1003','c1:title','hbase从入门到精通'
put 'book','1003','c1:author','李雷'
put 'book','1003','c1:price','029.50'

注意: rowkey1001 的记录,没有 price

代码

上接:hbase2.1.x java SingleColumnValueFilter 过滤

测试1:

hu.filter("book", "c1", "price",
        CompareOperator.EQUAL,
        "099.90");

查询结果:

1001    c1:author:lucy
c1:title:htlm从入门到放弃
------------------
1002    c1:author:lili
c1:price:099.90
c1:title:css从入门到放弃

原因:因为 rowkey 是 1001 的记录 没有 age ,在 hbase 中,默认 满足所有过滤条件

测试2:

列名写错了,写成大写的 PRICE

hu.filter("book", "c1", "PRICE",
        CompareOperator.EQUAL,
        "099.90");

查询出所有结果

原因:在 hbase 中,默认 满足所有过滤条件

解决

关键代码

filter.setFilterIfMissing(true);

解释:当 过滤条件的列 的 值为空 时,不满足条件,即:不包含该条记录

完整代码

public void filter(String tableName,String columnFamily,String column,CompareOperator co,String value) throws IOException {
    Table table = conn.getTable(TableName.valueOf(tableName));
    //创建Scan
    Scan scan=new Scan();
    SingleColumnValueFilter filter=new SingleColumnValueFilter(
            Bytes.toBytes(columnFamily),
            Bytes.toBytes(column),
            co,
            new BinaryComparator(Bytes.toBytes(value))); // 注意是字符串
    //========关键代码========
    //========关键代码========
    //========关键代码========
    filter.setFilterIfMissing(true);
    //传入filter
    scan.setFilter(filter);
    //执行scan
    ResultScanner resultScanner = table.getScanner(scan);
    Iterator<Result> iterator = resultScanner.iterator();
    //迭代,在迭代时,ResultScanner会发送get请求
    while(iterator.hasNext()){
        Result result=iterator.next();
        //获取rowkey
        String rowkey=Bytes.toString(result.getRow());
        System.out.print(rowkey+"\t");
        //获取所有单元格
        List<Cell> list=result.listCells();
        for(Cell item:list){
//            String family=Bytes.toString(item.getFamilyArray(),item.getFamilyOffset(),item.getFamilyLength());
//            String qualifier=Bytes.toString(item.getQualifierArray(),item.getQualifierOffset(),item.getQualifierLength());
//            String value=Bytes.toString(item.getValueArray(),item.getValueOffset(),item.getValueLength());
            String family=Bytes.toString(CellUtil.cloneFamily(item));
            String qualifier=Bytes.toString(CellUtil.cloneQualifier(item));
            String valueDb=Bytes.toString(CellUtil.cloneValue(item));
            System.out.println(family+":"+qualifier+":"+valueDb);
        }
        System.out.println("------------------");
    }
    //用完要及时关闭,在迭代时,ResultScanner会发送get请求
    resultScanner.close();
    table.close();
}

测试1

hu.filter("book", "c1", "price",
        CompareOperator.EQUAL,
        "099.90");

查询结果:

1002    c1:author:lili
c1:price:099.90
c1:title:css从入门到放弃

正确

测试2

查询列写成大写的 PRICE

hu.filter("book", "c1", "PRICE",
        CompareOperator.EQUAL,
        "099.90");

查询不到结果,这是正确的


原文出处:https://malaoshi.top/show_1IX2JiGBMM4G.html