1
votes

My Hbase table has a column which contains array of key-value pairs.

I read about row-key, column family or column, custom filter though,

I need scan columns which holding specific key name like...

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

I'm bushing around RDBMS wrapper but something more efficient way would exists, I think. Any advice would be appreciated.

2
so your value is json style string and you just wanna to find rows with value contains "KEY8" ?sel-fish

2 Answers

3
votes

Use SingleColumnValueFilter and SubstringComparator:

    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);

If you need to do it more precisely (eg. if the VALUE in your example contains KEY8, there will be unexpected results), you need to build a custom filter yourself.

1
votes

You can use a RowPrefixFilter.

You use the HBase library for this using the Scan Object

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!
    }

This should return you the value of the rows with KEY8