2016-08-22 88 views
1

我想创建一个扫描仪,它会给我带有2个前缀过滤器的结果
例如,我希望所有行的键以字符串“x”开头或以字符串“y”。
目前我知道的只有一个前缀以下方式来做到这一点:
设置多个前缀行筛选器到扫描仪hbase java

scan.setRowPrefixFilter(prefixFiltet) 

回答

2

在这种情况下,你不能使用setRowPrefixFilter API,你必须使用更普遍setFilter API,是这样的:

scan.setFilter(
    new FilterList(
    FilterList.Operator.MUST_PASS_ONE, 
    new PrefixFilter('xx'), 
    new PrefixFilter('yy') 
) 
); 
+1

关于此解决方案性能的说明:您正在执行全表扫描并将所有行通过这些过滤器。一般来说,这是非常低效的。对于大型表格,只有少数使用'scan.setRowPrefixFilter(prefix)'进行多次扫描的前缀可能会更快。 –

2

我刚刚试过,但它似乎并不您可以添加一个正则表达式的RowPrefixFilter,所以我猜解是使用两个请求

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

这会得到你想要的行。

+0

如果你这样做,只会返回以“y”开头的键,因为你重写了“x”。我的目标是获得1个扫描对象,这将有两个键的结果。 – MosheCh

+0

糟糕,我忘了补充说你应该分别执行每次扫描。尝试执行一个,存储结果并添加第二个结果 –

1

我有实施一批集前缀过滤器,也许可以帮助你

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