Correct way to get Region Name by using hbase API
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.
void getRegionOfTable(String tabName){ org.apache.hadoop.hbase.TableName tn = org.apache.hadoop.hbase.TableName.valueOf(tabName); org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create(); HRegionInfo ob; try{ HBaseAdmin hba = new HBaseAdmin(config); List<HRegionInfo> lr = hba.getTableRegions(tn); Iterator<HRegionInfo> ir = lr.iterator(); while(ir.hasNext()){ ob = ir.next(); System.out.println(ob.getRegionNameAsString()); } hba.close(); }catch(Exception ex){ ex.printStackTrace(); }}
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.