如何将几个QualifierFilter应用到HBase中的一行 [英] How to apply several QualifierFilter to a row in HBase

查看:261
本文介绍了如何将几个QualifierFilter应用到HBase中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望用两个QualifierFilter过滤HBase表上的扫描。
意味着我们只想得到具有某个列col_A AND (!)的某个列的某些行'col_B'。
$ b

我们目前的方法是这样的:

pre $ Filter $ FilterList = new FilterList(FilterList.Operator。 MUST_PASS_ALL);
Filter filter1 = new QualifierFilter(CompareOp.EQUAL,new BinaryComparator(col_A.getBytes()));
filterList.addFilter(filter1);
Filter filter2 = new QualifierFilter(CompareOp.EQUAL,new BinaryComparator(col_B.getBytes()));
filterList.addFilter(filter2);

扫描扫描=新扫描();
scan.setFilter(filterList);
...

ResultScanner不会从此扫描中返回任何结果, HBase表中有两列col_A和col_B的行。

如果我们只将filter1应用于扫描,一切正常,我们得到所有有'col_A'的行。
如果我们只将filter2应用于扫描,它是相同的。我们得到所有有'col_B'的行。

只有我们结合这两个过滤器,我们才会得到任何结果。

什么才是正确的方法来获得只有col_A和col_B表中的行?

解决方案

您可以通过定义以下过滤器来实现此目的:

  List< Filter> filters = new ArrayList< Filter>(2); 
byte [] colfam = Bytes.toBytes(c);
byte [] fakeValue = Bytes.toBytes(DOESNOTEXIST);
byte [] colA = Bytes.toBytes(col_A);
byte [] colB = Bytes.toBytes(col_B);

SingleColumnValueFilter filter1 =
SingleColumnValueFilter(colfam,colA,CompareOp.NOT_EQUAL,fakeValue);
filter1.setFilterIfMissing(true);
filters.add(filter1);

SingleColumnValueFilter filter2 =
SingleColumnValueFilter(colfam,colB,CompareOp.NOT_EQUAL,fakeValue);
filter2.setFilterIfMissing(true);
filters.add(filter2);

FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL,filters);
扫描扫描=新扫描();
scan.setFilter(filterList);

这里的想法是定义一个 SingleColumnValueFilter 每列都在寻找,每个都有一个假值和一个 CompareOp.NOT_EQUAL 操作符。即:
这样的SingleColumnValueFilter将返回给定名称的所有列。



来源: http://mapredit.blogspot.com/2012/05/using-filters-in-hbase-to- match-two.html


we would like to filter a scan on a HBase table with two QualifierFilters. Means we would like to only get the rows of the table which do have a certain column 'col_A' AND (!) a certain other column 'col_B'.

Our current approach looks like this:

FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
Filter filter1 = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator("col_A".getBytes()));
filterList.addFilter(filter1);
Filter filter2 = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator("col_B".getBytes()));
filterList.addFilter(filter2);

Scan scan = new Scan();
scan.setFilter(filterList);
... 

The ResultScanner does not return any results from this scan although there are several rows in the HBase table which do have both columns 'col_A' and 'col_B'.

If we only apply filter1 to the scan everything works fine an we do get all the rows which have 'col_A'. If we only apply filter2 to the scan it is the same. We do get all rows which have 'col_B'.

Only if we combine these two filters we do not get any results.

What would be the right way to get only the rows from the table which do have col_A AND col_B?

解决方案

You can achieve this by defining the following filters:

List<Filter> filters = new ArrayList<Filter>(2);
byte[] colfam = Bytes.toBytes("c");
byte[] fakeValue = Bytes.toBytes("DOESNOTEXIST");
byte[] colA = Bytes.toBytes("col_A");
byte[] colB = Bytes.toBytes("col_B");

SingleColumnValueFilter filter1 = 
    new SingleColumnValueFilter(colfam, colA , CompareOp.NOT_EQUAL, fakeValue);  
filter1.setFilterIfMissing(true);
filters.add(filter1);

SingleColumnValueFilter filter2 = 
    new SingleColumnValueFilter(colfam, colB, CompareOp.NOT_EQUAL, fakeValue);          
filter2.setFilterIfMissing(true);
filters.add(filter2);

FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, filters);
Scan scan = new Scan();
scan.setFilter(filterList);

The idea here is to define one SingleColumnValueFilter per column you are looking for, each with a fake value and a CompareOp.NOT_EQUAL operator. I.e: such a SingleColumnValueFilter will return all columns for a given name.

Source: http://mapredit.blogspot.com/2012/05/using-filters-in-hbase-to-match-two.html

这篇关于如何将几个QualifierFilter应用到HBase中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆