Reputation: 1254
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.
Upvotes: 1
Views: 1363
Reputation: 4476
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.
Upvotes: 3
Reputation: 7742
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
Upvotes: 1