4

我在 col 系列 (CF) 中有 3 个 cols {col1, col2, col3}。我想编写一个扫描,以便我选择所有具有col1='val1' and col2='val2' and (col3 is missing or col3 is null).

在 Java 中执行此操作 - 抱歉,我对 hbase 完全陌生 ;-)

4

2 回答 2

3

我自己在网上找到答案时遇到了很多麻烦。我终于想通了,它很简单——只是没有答案:

    Scan scanner = new Scan();

    SingleColumnValueFilter filter = new SingleColumnValueFilter(
        Bytes.toBytes( "some family" ),
        Bytes.toBytes( "some column" ),
        CompareFilter.CompareOp.EQUAL,
        Bytes.toBytes( "" )
    );
    filter.setFilterIfMissing( false );

    scanner.setFilter( filter );
    return scanner;

该解决方案的关键部分是设置setFilterIfMissing为 false 并与空字符串进行比较。

于 2016-07-25T21:48:39.477 回答
0

我认为以下代码可以帮助您:

    FilterList filterList = new FilterList();
    SingleColumnValueFilter filter1 = new SingleColumnValueFilter(Bytes.toBytes("someFamily"), Bytes.toBytes("col1"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("val1"));
    filter1.setFilterIfMissing(true);
    filterList.addFilter(filter1);

    SingleColumnValueFilter filter2 = new SingleColumnValueFilter(Bytes.toBytes("someFamily"), Bytes.toBytes("col2"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("val2"));
    filter2.setFilterIfMissing(true);
    filterList.addFilter(filter2);

    SingleColumnValueFilter filter3 = new SingleColumnValueFilter(Bytes.toBytes("someFamily"), Bytes.toBytes("col3"), CompareFilter.CompareOp.EQUAL, new NullComparator());
    filter3.setFilterIfMissing(false);
    filterList.addFilter(filter3);

    Scan scan = new Scan();
    scan.setFilter(filterList);
    ResultScanner resultScanner = table.getScanner(scan);
    for (Result res : resultScanner) {
        System.out.println(res);
    }
    resultScanner.close();
于 2016-07-27T08:10:38.740 回答