简体   繁体   中英

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM