Search code examples
javadatabaseeclipsehbase

How to get all the rows given a part of the row key in Hbase


I have the following table structure in Hbase:

Row               column+cell

Mary_Ann_05/10/2013 column=cf:verified, timestamp=234454454,value=2,2013-02-12  
Mary_Ann_06/10/2013 column=cf:verified, timestamp=2345454454,value=3,2013-02-12 
Mary_Ann_07/10/2013 column=cf:verified, timestamp=2345454522454,value=4,2013-02-12 
Mary_Ann_08/10/2013 column=cf:verified, timestamp=23433333454,value=1,2013-12-12 

I want to retrieve all the records that start with Mary_Ann using java. How do I do that?


Solution

  • You could achieve that using PrefixFilter. Given a prefix, specified when you instantiate the filter instance, all rows that match this prefix are returned to the client. The constructor is : public PrefixFilter(byte[] prefix)

    Usage :

    Filter filter = new PrefixFilter(Bytes.toBytes("Mary_Ann"));
    Scan scan = new Scan();
    scan.setFilter(filter);
    ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
         for (KeyValue kv : result.raw()) {
            System.out.println("KV: " + kv + ", Value: " +
            Bytes.toString(kv.getValue()));
         }
    }
    scanner.close();
    

    HTH