I am trying to fetch "Region Name" for a "table" using HBase API.
The setup is mentioned below:
Hbase pseudo-distributed installation (version 0.98.7).
Hadoop 2.5.1 installation.
The Hbase contains very few tables for testing purpose. And information about available regions are shown below from the web UI.
"region name" corresponding to the table "test_table" has been highlighted purposefully.
Now, I have been trying to get these region information from the java based API of hbase using below codes.
void scanTable(String tabName){
org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
try{
HTable table = new HTable(config, tabName);
org.apache.hadoop.hbase.TableName tn = table.getName();
HRegionInfo hr = new HRegionInfo(tn);
System.out.println(hr.getRegionNameAsString());
table.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Whenever, I pass a table name, say "test_table", the regionName is returned differently on every run.
RUN 1:
test_table,,1419247657866.77b98d085239ed8668596ea659a7ad7d.
RUN 2:
test_table,,1419247839479.d3097b0f4b407ca827e9fa3773b4d7c7.
RUN 3:
test_table,,1419247859921.e1e39678fa724d7168cd4100289c4234.
I assume that I am using wrong method to generate "region_name" or my approach is wrong. Please help me to get the region information for given table name.
There is a getTableRegions() in HBaseAdmin which returns all the region info for the table name you want.
List getTableRegions(final TableName tableName)
Below is the method that outputs region name for a given table name.
Your code produce a different result every time, because you are building a new "region" with a different timestamp every time. Also that code assumes that your table has a single region.