如何过滤 HBase 中列上的键值数组?

How to filter array of key-values on a column in HBase?

我的 Hbase table 有一列包含键值对数组。

虽然我读到了 row-keycolumn familycolumncustom filter

我需要扫描包含特定键名的列,例如...

ROW1 , CF1, DATA_COLUMN : {KEY1:VALUE, KEY2:VALUE, KEY3:VALUE }
ROW2 , CF1, DATA_COLUMN : {KEY1:VALUE}
ROW3 , CF1, DATA_COLUMN : {KEY1:VALUE, KEY5:VALUE}
ROW4 , CF1, DATA_COLUMN : {KEY8:VALUE} <--- Only needed row with KEY8 value set

我正在绕过 RDBMS 包装器,但我认为存在更有效的方法。如有任何建议,我们将不胜感激。

您可以使用 RowPrefixFilter。

您使用 HBase 库来使用扫描对象

this.configuration = HBaseConfiguration.create();
this.connection = ConnectionFactory.createConnection(this.configuration);


String columnFamily = "CF1";
String columnName = "name";
String pattern = "KEY8";

Table table = this.connection.getTable(TableName.valueOf("myTable"));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName));
scan.setRowPrefixFilter(Bytes.toBytes(pattern));
ResultScanner rs = table.getScanner(scan);
try {
    for (Result r = rs.next(); r != null; r = rs.next()) {
        byte[] value = r.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName));
        String valueStr = Bytes.toString(value);
        System.out.println("row key "+new String(r.getRow()));
        System.out.println("Scan result :" + valueStr);
        }
    } finally {
        rs.close(); // always close the ResultScanner!
    }

这应该 return 你的值是 KEY8

的行

使用SingleColumnValueFilterSubstringComparator:

    SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
            Bytes.toBytes("CF1"),
            Bytes.toBytes("DATA_COLUMN"),
            CompareFilter.CompareOp.EQUAL,
            new SubstringComparator("KEY8")
    );
    Scan scan = new Scan();
    scan.setFilter(singleColumnValueFilter);
    ResultScanner resultScanner = table.getScanner(scan);

如果你需要做的更精确(例如,如果你的例子中的VALUE包含KEY8,会有意想不到的结果),你需要自己构建一个自定义过滤器。