Determine the number of keys in each region of the HBase table in Java

Let’s imagine that you need to know the actual distribution of keys by regions of the HBase table. There is no direct way to find it. But you can use this workaround: you need to send scan queries with region boundaries:

private AtomicLong count = new AtomicLong(0L);

private void printRegionStats() throws IOException {
  List<HRegionInfo> regions = connection.getAdmin().getTableRegions(tableName);
  try (Table table = connection.getTable(tableName)) {
    regions.forEach(r -> {
      Scan scan = new Scan();
      scan.setStartRow(r.getStartKey());
      scan.setStopRow(r.getEndKey());
      try (ResultScanner resultScanner = table.getScanner(scan)) {
        resultScanner.forEach(result -> { count.incrementAndGet() });
      } catch (IOException e) {
        e.printStackTrace();
      }
      log.error("found new region. keys count: {}", counter);
      counter.set(0L);
    });
  }
}

This code will display line by line the number of keys in each region of the table.

Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at promark33@gmail.com.

If I saved your day, you can support me 🤝

Leave a Reply

Your email address will not be published. Required fields are marked *