Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hbase scan with offset

Tags:

hadoop

hive

hbase

Is there a way to scan a HBase table getting, for example, the first 100 results, then later get the next 100 and so on... Just like in SQL we do with LIMIT and OFFSET? My row keys are uuid

like image 738
Mark Avatar asked May 04 '14 04:05

Mark


People also ask

How do I scan a HBase table?

To get all columns from all rows of a Table, create an instance with no constraints; use the Scan() constructor. To constrain the scan to specific column families, call addFamily for each family to retrieve on your Scan instance. To get specific columns, call addColumn for each column to retrieve.

What is filter in HBase?

It compares each value with the comparator using the compare operator and if the comparison returns true, it returns that key-value. PrefixFilter: takes a single argument, a prefix of a row key. It returns only those key-values present in a row that start with the specified row prefix.


1 Answers

You can do it multiple ways. The easiest one is a page filter. Below is the code example from HBase: The Definitive Guide, page 150.


private static final byte[] POSTFIX = new byte[] { 0x00 };
Filter filter = new PageFilter(15);
int totalRows = 0; byte[] lastRow = null; 
while (true) {
  Scan scan = new Scan(); 
  scan.setFilter(filter); 
  if (lastRow != null) {
    byte[] startRow = Bytes.add(lastRow, POSTFIX); 
    System.out.println("start row: " + Bytes.toStringBinary(startRow)); 
    scan.setStartRow(startRow);
  }

  ResultScanner scanner = table.getScanner(scan); 
  int localRows = 0;
  
  Result result;
  
  while ((result = scanner.next()) != null) {
     System.out.println(localRows++ + ": " + result); 
     totalRows++;

     lastRow = result.getRow();
  }

  scanner.close();

  if (localRows == 0) break;
}


System.out.println("total rows: " + totalRows);

Or you can set catching on scan for the limit you want and then change the start row to the last row + 1 from the prev scan for every get.

like image 189
Arun J Avatar answered Oct 26 '22 06:10

Arun J