从HBase中检索除特定列族的值之外的所有值

时间:2013-05-01 10:18:56

标签: java hbase

我正在编写一个Java应用程序,用于从HBase数据库中检索和显示数据。

在编写用于检索行的Get方法时,我希望获取该行的所有数据,但不包括特定列族(“大”列族)的值。注意:我需要检索该系列中的列名(限定符?),因为它们包含有价值的信息。

是否可以为此编写过滤器?

我有两个解决方案。第一个不起作用,第二个很慢。

第一种解决方案(使用复合滤波器):

HTable table = getTable();
Get get = new Get(row);
FilterList filter = new FilterList(FilterList.Operator.MUST_PASS_ONE);

FilterList subFilterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
subFilterList.addFilter(new KeyOnlyFilter());
subFilterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));

filter.addFilter(subFilterList);
filter.addFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));

get.setFilter(filter);
retrieveAndUseResult(table, get);

这个解决方案在概念上和实践中都不起作用 - 但也许我使用复合FilterList在正确的轨道上?

第二个解决方案(使用两个获取):

HTable table = getTable();
Get get = new Get(row);
// exclude the entire "big" column family 
get.setFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
retrieveAndUseResult(table, get);

Get get2 = new Get(row);
// include the "big" column family, but only retrieve the key 
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new KeyOnlyFilter());
filterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
get2.setFilter(filterList);
retrieveAndUseResult(table, get2);

这很有效,但我不愿意只做一次。

1 个答案:

答案 0 :(得分:0)

我最终使用了第二种解决方案的变体 - 使用两个获取。但我使用批量获取列表来加快速度。

代码:

HTable table = getTable();

Get get = new Get(row);
// exclude the entire "big" column family 
get.setFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));

Get get2 = new Get(row);
// include the "big" column family, but only retrieve the key 
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new KeyOnlyFilter());
filterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
get2.setFilter(filterList);

List<Get> getList = new ArrayList<Get>();
getList.add(get);
getList.add(get2);
retrieveAndUseResults(table, getList);
相关问题