Check whether a value exists in an HBase table
Check whether a value exists in an HBase table
I've got an HBase table with data that looks like:
key1 c:hasErrors false
key2 c:hasErrors true
key3 c:hasErrors false
I want to check whether the column qualifier hasErrors
has a value true
anywhere in the table.
hasErrors
true
Obviously I can do a scan:
Scan scan = new Scan();
scan.addColumn(colFam, colQual);
Filter filter = new SingleColumnValueFilter(colFam, colQual, CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("true")));
scan.setFilter(filter);
ResultScanner results = table.scan(scan, auditTableName);
but that's undesirable because any matching rows will be pulled back to my app, and I have to check whether results.next() != null
.
results.next() != null
Is there a way to just have a boolean returned that tells me whether or not the value is present in at least one row?
@Harold thanks, I haven't found another way either. Luckily it's not a huge issue (I'm scanning with a row key prefix, and the scan isn't run particularly often).
– Ben Watson
Jun 29 at 9:34
1 Answer
1
Take a look at
org.apache.hadoop.hbase.filter.SingleColumnValueFilter
/**
* Set whether entire row should be filtered if column is not found.
*
* If true, the entire row will be skipped if the column is not found.
*
* If false, the row will pass if the column is not found. This is default.
* @param filterIfMissing flag
*/
public void setFilterIfMissing(boolean filterIfMissing) {
this.filterIfMissing = filterIfMissing;
}
According to docs, it will not return rows that did not match.
P.S. If you use Get, you have setCheckExistenceOnly
method for the similar purpose
setCheckExistenceOnly
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I don’t any other way and scans should be avoided for performances reasons. Consider having another HBase table to track counts of hasErrors, and populate it together with the initial table.
– Harold
Jun 29 at 9:29