简体   繁体   中英

Hbase Scan Filter for Records that are Missing Column or the Column Value is null

I have 3 cols {col1, col2, col3} in a col family (CF). I'd like to write a scan such that I select all rows that have col1='val1' and col2='val2' and (col3 is missing or col3 is null) .

Doing this in Java - sorry I'm totally new to hbase ;-)

I had a lot of trouble finding an answer to this myself online. I finally figured it out, and its quite simple - just unanswered:

    Scan scanner = new Scan();

    SingleColumnValueFilter filter = new SingleColumnValueFilter(
        Bytes.toBytes( "some family" ),
        Bytes.toBytes( "some column" ),
        CompareFilter.CompareOp.EQUAL,
        Bytes.toBytes( "" )
    );
    filter.setFilterIfMissing( false );

    scanner.setFilter( filter );
    return scanner;

The key parts of the solution are setting setFilterIfMissing to false and comparing with an empty string.

I think the following code can help you:

    FilterList filterList = new FilterList();
    SingleColumnValueFilter filter1 = new SingleColumnValueFilter(Bytes.toBytes("someFamily"), Bytes.toBytes("col1"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("val1"));
    filter1.setFilterIfMissing(true);
    filterList.addFilter(filter1);

    SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("someFamily"), Bytes.toBytes("col2"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("val2"));
    filter2.setFilterIfMissing(true);
    filterList.addFilter(filter2);

    SingleColumnValueFilter filter3 = new SingleColumnValueFilter(Bytes.toBytes("someFamily"), Bytes.toBytes("col3"), CompareFilter.CompareOp.EQUAL, new NullComparator());
    filter3.setFilterIfMissing(false);
    filterList.addFilter(filter3);

    Scan scan = new Scan();
    scan.setFilter(filterList);
    ResultScanner resultScanner = table.getScanner(scan);
    for (Result res : resultScanner) {
        System.out.println(res);
    }
    resultScanner.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM