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();