简体   繁体   中英

How to filter the result of a Scan in Hbase

I'm new to HBase and I'm writing a Java program which gets some data from a HBase table. The rowkeys have format #### - ### where # represents a number between 0-9 . I would like to retrieve only rows starting with a specified pattern, let's say 2345 -. I found some example to retrieve a range of rows (using Scan(byte[] startRow, byte[] stopRow) but this is not useful in my scenario.

Could someone help me with that?

Why do you say it's not useful in your scenario?.

If you want to retrieve rows starting with 2345 you can easily do a partial scan providing start & stop row keys:

// the dot is greater than the hyphen, Stop Row is exclusive.
// the scan will stop when the prefix is not "2345-"
Scan scan = new Scan( "2345-".getBytes(), "2345.".getBytes() ); 
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
      // ...
}

Another option:

// the scan will stop when the prefix is not "2345"
Scan scan = new Scan( "2345".getBytes(), "2346".getBytes() ); 
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
      // ...
}

Please remind that it's not mandatory for the start & stop rows to exist, they're just boundaries.

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