Skip to main content
kpalanisamy
Gold ⭐️
Gold ⭐️
Jun 5, 2018 4:23 PM UTC

How to find/tell region name for given hbase rowkey

  • Oct 5, 2026 8:41 AM UTC
  • 2 replies
  • 8498 views
  • Translate All

Simple API to find region name for given row key. It will also give you hint about the region - Where key will be placed.

To compile class:

$JAVA_HOME/bin/javac -cp `hbase classpath`: Regionfinder.java

To run tool:

$JAVA_HOME/bin/java -cp `hbase classpath`: Regionfinder <tablename> <rowkey>

Regionfinder.java

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HRegionLocation;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class Regionfinder {
private static void usage(String[] args) {
if (args.length < 2) {
System.out.println("$JAVA_HOME/bin/java -cp `hbase classpath`: Regionfinder <tablename> <rowkey>");
System.exit(-1);
}
}

public static void main(String[] args) throws IOException {
usage(args);
TableName tablename = TableName.valueOf(args[0]);
String rowkey = args[1];
byte[] keyInBytes = Bytes.toBytes(rowkey);
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(tablename);
RegionLocator regionLocater = connection.getRegionLocator(tablename);
HRegionLocation regionLocation = regionLocater.getRegionLocation(keyInBytes);
Result result = table.get(new Get(keyInBytes));
if(result.isEmpty()){
System.out.println("Rowkey "+rowkey+" is not exist in any region. It will be placed in region : "+regionLocation.getRegionInfo().getRegionNameAsString());
}else{
System.out.println("Table Name = " + tablename + "\n" + "Row Key = " + rowkey + "\n" + "Region Server = "
+ regionLocation.getServerName() + "\n" + "Region Name = "
+ regionLocation.getRegionInfo().getRegionNameAsString() + "\n" + "Encoded Region Name = "
+ regionLocation.getRegionInfo().getEncodedName());
}
}
}

Ex Output:
#java -cp `hbase classpath`: Regionfinder sp 100
Table Name = sp
Row Key = 100
Region Server = hwx2633.openstacklocal,16020,1528109888044
Region Name = sp,100,1521182497105.6e87f8a4f3bf7c2762d644dba8e98022.
Encoded Region Name = 6e87f8a4f3bf7c2762d644dba8e98022

Non existing row key:
#java -cp `hbase classpath`: Regionfinder sp 10
Rowkey 10 is not exist in any region. It will be placed in region : sp,10,1521182497105.ecc8568cb94776ad83ee04cbb422bff0

2 replies

VR46
Gold ⭐️⭐️
Gold ⭐️⭐️
Jun 6, 2018 1:21 AM UTC
@Karthik Palanisamy,

This is really a good piece of information. Thanks for sharing. Keep them coming !

Translate
9een
Gold ⭐️
Gold ⭐️
May 1, 2024 10:59 AM UTC

@kpalanisamy 
➤ We also have a alternate hbase shell native approach through which we can determine the RegionName and RegionServer from the rowkey

$ locate_region 'namespace:tablename','rowkey'

 HOST                             REGION
Regionserver-name:16 {ENCODED => regionName, NAME => 'namespace:tablename,rowkey.regionName.', STARTKEY => 'f0046', ENDKEY => 'f0245cf'}

 1 row(s)
 Took 0.6760 seconds
 => #<Java::OrgApacheHadoopHbase::HRegionLocation:0x4070c4ff>

 

Translate