Search code examples
hbaseschema-design

HBase: Scan column qualifiers - is it possible?


I have some various IDs related to each row: let's say, 1001, and 1002. Each of these IDs has a similar set of attributes.

So my thought is to have one column family ('cf') with column qualifiers as such:

cf:1001-attribute1 -> 'value a'
cf:1001-attribute2 -> 'value b'
cf:1001-attribute3 -> 'value c'
cf:1002-attribute1 -> 'value d'
cf:1002-attribute2 -> 'value e'
cf:1002-attribute3 -> 'value f'

Am I able to scan for: row(x), column family (cf), columns (1001*) so that I pick up a map of all the attributes for 1001 in this case?

Obviously I can do this for row keys, I'm not sure if column qualifiers work the same.


Solution

  • You can use the ColumnPrefixFilter

    HTableInterface t = ...;
    byte[] prefix = Bytes.toBytes("1001");
    Scan scan = new Scan() 
    Filter f = new ColumnPrefixFilter(prefix);
    scan.setFilter(f);
    scan.setBatch(20); // set this if there could be many columns returned
    ResultScanner rs = t.getScanner(scan);
    for (Result r = rs.next(); r != null; r = rs.next()) {
      for (KeyValue kv : r.raw()) {
        // each kv represents a column
      }
    }
    rs.close();