基于两个单元扫描HBase

时间:2015-02-26 17:42:12

标签: groovy hbase

假设我有两个HBase细胞:

  • X:Y
  • X位:Z

如何执行此SQL的等效操作:

SELECT * FROM some_table WHERE x_y = ? AND x_z = ?

这是我用于生成基本过滤器的(Groovy)代码:

static SingleColumnValueFilter makeColumnFilter(String family, String qualifier, String expectedValue) {
    new SingleColumnValueFilter (
            Bytes.toBytes(family),
            Bytes.toBytes(qualifier),
            CompareFilter.CompareOp.valueOf('EQUAL'),
            new SubstringComparator(expectedValue))
}

def filterz = filters.collect {
    makeColumnFilter(it.family, it.qualifier, it.expectedValue)
}
def fl = new FilterList(filterz)
def scan = new Scan()
scan.setFilter(fl)
def family = 'x'.bytes
t.getScanner(scan).each {
    println "${Bytes.toString(it.getValue(family, 'y'.bytes))}"
    count++
}

print语句只显示空值,即使我传入x表示系列值,y / z表示限定符。它似乎没有过滤值。我做错了什么?

2 个答案:

答案 0 :(得分:1)

如果使用setFilterIfMissing找不到列,则需要过滤行。

将makeColumnFilter更改为:

static SingleColumnValueFilter makeColumnFilter(String family, String qualifier, String expectedValue) {
    def colFilter = new SingleColumnValueFilter (
            Bytes.toBytes(family),
            Bytes.toBytes(qualifier),
            CompareFilter.CompareOp.valueOf('EQUAL'),
            new SubstringComparator(expectedValue))
    colFilter.setFilterIfMissing(true)
    colFilter
}

答案 1 :(得分:0)

我认为SingleColumnValueExcludeFilter应该完成这项工作。

    Scan scan = new Scan();
    SingleColumnValueExcludeFilter singleColumnValueFilterY = new SingleColumnValueExcludeFilter("x".getBytes(),
            "y".getBytes(), CompareOp.EQUAL, new BinaryComparator("valueY".getBytes()), true, true);
    SingleColumnValueExcludeFilter singleColumnValueFilterZ = new SingleColumnValueExcludeFilter("x".getBytes(),
            "z".getBytes(), CompareOp.EQUAL, new BinaryComparator("valueZ".getBytes()), true, true);

    FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
    filterList.addFilter(singleColumnValueFilterX);
    filterList.addFilter(singleColumnValueFilterY);

    scan.setFilter(filterList);

可以找到有关HBase过滤器的更详细文档here

希望这会有所帮助。