通过使用hbase API获取区域名称的正确方法 [英] Correct way to get Region Name by using hbase API

查看:180
本文介绍了通过使用hbase API获取区域名称的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



设置如下:


我试图使用HBase API为表格名称


  1. Hbase伪分布式安装(版本0.98.7)。

  2. Hadoop 2.5。 1安装。

Hbase包含很少的测试目的表。有关可用区域的信息显示在下面的Web UI中。





与表test_table对应的region name已被有目的地突出显示。



现在,我一直在尝试使用下面的代码从基于java的基于API的API获取这些区域信息。

  void scanTable(String tabName){

org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
尝试{
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();
}
}
}

每次,名称,例如test_table,每次运行时regionName都会以不同的方式返回。



运行1:

  TEST_TABLE ,, 1419247657866.77b98d085239ed8668596ea659a7ad7d。 

运行2:

  TEST_TABLE ,, 1419247839479.d3097b0f4b407ca827e9fa3773b4d7c7。 

运行3:

  TEST_TABLE ,, 1419247859921.e1e39678fa724d7168cd4100289c4234。 

我假设我使用错误的方法来生成region_name或我的方法错误。
请帮助我获取给定表名的区域信息。

解决方案

有一个getTableRegions()在HBaseAdmin它返回所需的表名称的所有区域信息。



List getTableRegions(final TableName tableName)



  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;
尝试{
HBaseAdmin hba = new HBaseAdmin(config);
列出< HRegionInfo> lr = hba.getTableRegions(tn);
迭代器< HRegionInfo> ir = lr.iterator();
while(ir.hasNext()){
ob = ir.next();
System.out.println(ob.getRegionNameAsString());
}
hba.close();
} catch(Exception ex){
ex.printStackTrace();




$ b你的代码每次都会产生不同的结果,因为你每次都在建立一个新的区域,时间戳不同。此外,该代码假定您的表具有单个区域。

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.

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

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.

这篇关于通过使用hbase API获取区域名称的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆