简体   繁体   中英

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

I have got the scan result for a given partial row key using prefix filter:

Row key examples: 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 can I specify multiple prefix filters that will go as input for the scan operation. The Result object should contain all the rows that have row key value with the given prefix, say 123 or 456.

I have tried the following answer which uses FilterList approach but couldn't get the required result:

Set Multiple prefix row filter to scanner hbase java

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

Please check this docs of filter list you might have NOT used 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)

Point to verify :

since You already used FilterList , I think you might have used default one ie MUST_PASS_ALL for which all prefix conditions needs to be met may be that's why its not giving result.

afore mentioned code should work.. Good luck

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