Community Articles

Find and share helpful community-sourced technical articles.
Announcements
Celebrating as our community reaches 100,000 members! Thank you!
Labels (1)
avatar
Expert Contributor

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
5,540 Views
Comments
avatar
Guru
@Karthik Palanisamy,

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