Correct way to get Region Name by using hbase API

2019-05-27 12:39发布

I am trying to fetch "Region Name" for a "table" using HBase API.

The setup is mentioned below:

  1. Hbase pseudo-distributed installation (version 0.98.7).

  2. 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.

enter image description here

"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.

1条回答
在下西门庆
2楼-- · 2019-05-27 13:00

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.

查看更多
登录 后发表回答