3

I want to create one scanner that will give me result with 2 prefix filters
For example I want all the rows that their key starts with the string "x" or start with the string "y".
Currently I know to do it only with one prefix with the following way:

scan.setRowPrefixFilter(prefixFiltet)
MosheCh
  • 99
  • 3
  • 12

3 Answers3

4

In this case you can't use the setRowPrefixFilter API, you have to use the more general setFilter API, something like:

scan.setFilter(
  new FilterList(
    FilterList.Operator.MUST_PASS_ONE, 
    new PrefixFilter('xx'), 
    new PrefixFilter('yy')
  )
);
  • 2
    A note about the performance of this solution: You are doing a full table scan and putting ALL rows through these filters. In general this is extremely inefficient. With a large table and only a handful of prefixes doing multiple scans using `scan.setRowPrefixFilter(prefix)` may be faster. – Niels Basjes Oct 04 '17 at 08:49
2

I have just tried but it doesn't seems you can add a regular expression to a RowPrefixFilter, so I guess the solution is to make two requests using

scan.setRowPrefixFilter("x")
scan.setRowPrefixFilter("y")

This will get you the rows you want.

Graham
  • 7,431
  • 18
  • 59
  • 84
Alexi Coard
  • 7,106
  • 12
  • 38
  • 58
  • If you do it like that it will return only keys that start with "y" because you override the "x". My goal is to get 1 Scan object that will have result of both keys. – MosheCh Aug 22 '16 at 16:03
  • Oops my bad, I forgot to add that you should execute each scan separately. Try to execute one, store the result, and add the result of the second one – Alexi Coard Aug 22 '16 at 16:50
0

I have implement a batch set prefix filter ,maybe can help you

    List<String> bindCodes = new ArrayList<>();
    bindCodes.add("CM0001");
    bindCodes.add("CE7563");
    bindCodes.add("DR6785");

    Scan scan = new Scan();
    scan.setCaching(50);//set get batch numbers
    //set Column
    scan.addColumn(HTableColumnEnum.GPS_CF_1.getCfName().getBytes(), LOCATION_CREATE_DATE_ARRAY);
    //set Family
    scan.addFamily(HTableColumnEnum.GPS_CF_1.getCfName().getBytes());

    //create filterList
    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
    //put mulit prefix row key
    bindCodes.forEach(s -> {
        filterList.addFilter(new PrefixFilter(Bytes.toBytes(s)));
    });

    //set filterList to scan
    scan.setFilter(filterList);
Ordiy
  • 89
  • 1
  • 3