简体   繁体   中英

Hbase scan with offset

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

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.

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