Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Community Articles

Find and share helpful community-sourced technical articles.
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!
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
8,477 Views
Comments
avatar
Guru
@Karthik Palanisamy,

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

avatar
Expert Contributor

@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>

 

Version history
Last update:
‎06-05-2018 09:23 AM
Updated by:
Contributors