将java远程连接到hbase

jv2fixgn  于 2021-06-10  发布在  Hbase
关注(0)|答案(2)|浏览(436)

我正在尝试将java连接到远程服务器中的hbase。
情况如下:
java类在我的本地计算机(192.168.111.165)中
hadoop文件系统和hbase位于另一台服务器(192.168.11.7)中
以下是我的hadoop的core-site.xml(192.168.11.7版本):

<configuration>
  <property>
        <name>fs.default.name</name>
        <value>hdfs://192.168.11.7:9000</value>
  </property>
</configuration>

这是我的hadoop的hdfs-site.xml(在192.168.11.7中):

<configuration>
  <property>
        <name>dfs.replication</name>
        <value>1</value>
  </property>
  <property>
    <name>dfs.name.dir</name>
    <value>file:///home/hadoop/hadoopinfra/hdfs/namenode</value>
  </property>

  <property>
    <name>dfs.data.dir</name>
    <value>file:///home/hadoop/hadoopinfra/hdfs/datanode</value>
  </property>   
</configuration>

以下是我的hbase的hbase-site.xml(在192.168.11.7中):

<configuration>
  <property>
    <name>hbase.rootdir</name>
    <value>hdfs://192.168.11.7:9000/hbase</value>
  </property>

  <property>
    <name>hbase.zookeeper.property.dataDir</name>
    <value>/home/hadoop/zookeeper</value>
  </property>

  <property>
    <name>hbase.cluster.distributed</name>
    <value>true</value>
  </property>
</configuration>

我已经成功地打开了hdfs和hbase(我可以在hbase shell中运行“list”命令)。注意,我在hbase shell中使用ssh从本地计算机运行“list”命令到192.168.11.7。从我的本地计算机(192.168.111.165)中,我可以使用浏览器访问hadoop文件系统和hbase(通过点击url:http://192.168.11.7:50070/,hadoop群集通过点击url:http://192.168.11.7:8088/,通过点击url:http://192.168.11.7:16010/)
现在,我在本地计算机(192.168.111.165)中开发了一个java代码,其中包含hbase-site.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  <configuration>
    <property>
      <name>hbase.rootdir</name>
      <value>hdfs://192.168.11.7:9000/hbase</value>
    </property>
</configuration>

我的主要代码如下:

private static Configuration conf = null;

public static void main(String[] args) throws Exception {
    try {
        conf = HBaseConfiguration.create();
        String tablename = "scores";
        String[] familys = { "grade", "course" };
        creatTable(tablename, familys);
    } catch (Exception e) {
        e.printStackTrace();

        throw e;
    }
}

public void creatTable(String tableName, String[] familys)
        throws Exception {
    HBaseAdmin admin = new HBaseAdmin(conf);
    if (admin.tableExists(tableName)) {
        System.out.println("table already exists!");
    } else {
        HTableDescriptor tableDesc = new HTableDescriptor(tableName);
        for (int i = 0; i < familys.length; i++) {
            tableDesc.addFamily(new HColumnDescriptor(familys[i]));
        }
        admin.createTable(tableDesc);
        System.out.println("create table " + tableName + " ok.");
    }
}

当我运行代码时,得到一个异常,它有stacktrace:

org.apache.hadoop.hbase.client.RetriesExhaustedException: Can't get the locations
at org.apache.hadoop.hbase.client.RpcRetryingCallerWithReadReplicas.getRegionLocations(RpcRetryingCallerWithReadReplicas.java:319)
at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas.call(ScannerCallableWithReplicas.java:156)
at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas.call(ScannerCallableWithReplicas.java:60)
at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithoutRetries(RpcRetryingCaller.java:210)
at org.apache.hadoop.hbase.client.ClientScanner.call(ClientScanner.java:326)
at org.apache.hadoop.hbase.client.ClientScanner.nextScanner(ClientScanner.java:301)
at org.apache.hadoop.hbase.client.ClientScanner.initializeScannerInConstruction(ClientScanner.java:166)
at org.apache.hadoop.hbase.client.ClientScanner.<init>(ClientScanner.java:161)
at org.apache.hadoop.hbase.client.HTable.getScanner(HTable.java:797)
at org.apache.hadoop.hbase.MetaTableAccessor.fullScan(MetaTableAccessor.java:602)
at org.apache.hadoop.hbase.MetaTableAccessor.tableExists(MetaTableAccessor.java:366)
at org.apache.hadoop.hbase.client.HBaseAdmin.tableExists(HBaseAdmin.java:406)
at org.apache.hadoop.hbase.client.HBaseAdmin.tableExists(HBaseAdmin.java:416)
at com.volt.kismistest.handler.TestHBaseHandler.creatTable(TestHBaseHandler.java:82)
at com.volt.kismistest.handler.TestHBaseHandler.doAction(TestHBaseHandler.java:47)
at com.volt.kismistest.handler.TestHBaseHandler.doAction(TestHBaseHandler.java:1)

请注意,例外情况是:
testhbasehandler是我的java类的名称
testhbasehandler。java:82 is :if(admin.tableexists(tablename)){在createTable方法上
testhbasehandler。java:47 is :creattable(表名,族);论主要方法
有人能帮我解决这个问题吗?
非常感谢你

tyu7yeag

tyu7yeag1#

这里只是猜测,因为许多问题可能取决于hbase的安装方式/位置或您的网络配置。
在hbase中,大部分工作由从属节点完成,而主节点只是您的主要“联系点”。大多数情况下,您会点击主节点,它只返回从节点的ip(很可能是内部ip),该ip应该完成操作(当然是读/写,也可能是create table)。如果您的客户端无法解析或无法访问该从属ip,则可能会遇到连接问题。你得到的例外可能会有所不同。
尝试事项:
能否从主节点上的hbase shell创建表?
注: list 是不够的,因为它可能只是由主节点服务。。。
命令是否通过restapi工作(可以从主shell启动)

5m1hhzi4

5m1hhzi42#

你说你可以跑 list ,但你能跑吗 create 以及 scan 在特定的table上?您可能忘记启动区域服务器。我会这样做: create testtable, {NAME => 'testcf'} 看看这是否也会在shell中创建它。如果这样行得通,快跑 scan 'testtable' 以验证是否可以扫描它。
也可能是防火墙问题。区域服务器可能已启动,但其端口已通过防火墙连接。
另一件事,您也可以致电hbaseadmin#available,检查hbase是否已启动并正在从客户端运行。

相关问题