Java – HBase: How to specify multiple prefix filters in a single scan operation

HBase: How to specify multiple prefix filters in a single scan operation… here is a solution to the problem.

HBase: How to specify multiple prefix filters in a single scan operation

I used the prefix filter to get the scan result for the given partial row key:

Examples of row keys: 123_abc, 456_def, 789_ghi

var prefix=Bytes.toBytes("123")
var scan = new Scan(prefix)
var prefixFilter = new PrefixFilter(prefix)
scan.setFilter(prefixFilter)
var resultScanner = table.getScanner(scan)

Now my question is how to specify multiple prefix filters as input to the scan operation. The result object should contain all rows with a row key value for a given prefix, such as 123 or 456.

I tried the following answer that uses the FilterList method but can’t get the desired result:

Set Multiple prefix row filter to scanner hbase java

Any help (in Scala or Java) would be appreciated. Thank you.

Solution

Please check this docs of filter list You may not be using the correct option….

FilterList.Operator.MUST_PASS_ALL (AND) or
FilterList.Operator.MUST_PASS_ONE (OR). Since you can use Filter Lists
as children of Filter Lists, you can create a hierarchy of filters to
be evaluated. FilterList.Operator.MUST_PASS_ALL evaluates lazily:
evaluation stops as soon as one filter does not include the KeyValue.
FilterList.Operator.MUST_PASS_ONE evaluates non-lazily: all filters
are always evaluated. Defaults to FilterList.Operator.MUST_PASS_ALL.

 /* FilterList.Operator.MUST_PASS_ALL by default */
      FilterList allFilters = new FilterList(FilterList.Operator.MUST_PASS_ONE);
      allFilters.addFilter(new PrefixFilter(Bytes.toBytes("123")));
     allFilters.addFilter(new PrefixFilter(Bytes.toBytes("456")));
     allFilters.addFilter(new PrefixFilter(Bytes.toBytes("678")));
    scan.setFilter(allFilters);

var resultScanner = table.getScanner(scan)

Verification point:

Since you’ve already used FilterList, I think you’ve probably used the default, which is MUST_PASS_ALL, which is required for all prefix conditions
Encountered may be the reason why it does not give results.

The previously mentioned code should work: Good luck

Related Problems and Solutions