Java – Access remote hbase in Java, masterNotRunning

Access remote hbase in Java, masterNotRunning… here is a solution to the problem.

Access remote hbase in Java, masterNotRunning

Recently, I need to use HBase as a database, so I want to study hbase. But recently I encountered a problem and searched for many days without finding an answer.
Let’s start with my machine. An ordinary PC, system win7, installed a virtual machine under win7, the virtual machine running ubuntu 10, with Hadoop and HBase, all run successfully. The HBase shell and JAVA API in the virtual machine run HBase successfully.

BUT THE PROBLEM IS THAT I WANT TO ACCESS HBASE ON A PC WITH JAVA API (WIN7) AND IT FAILS.
The .xml of Hbase-site is as follows

<property>
   <name>hbase.cluster.distributed</name>
   <value>false</value>
</property>
<property>
   <name>hbase.tmp.dir</name>
   <value>/opt/tmp</value>
</property>
<property>
   <name>hbase.rootdir</name>
   <value>hdfs://192.168.235.134:9000/hbase</value>
</property>
<property>
   <name>hbase.zookeeper.quorum</name>
   <value>192.168.235.134</value>
</property>

/

etc/hosts is as follows

192.168.235.134 localhost ubuntu

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

PC (win7) JAVA API HBASE-SITE .xml AS FOLLOWS

<property>
   <name>hbase.cluster.distributed</name>
   <value>false</value>
</property>
<property>
   <name>hbase.rootdir</name>
   <value>hdfs://192.168.235.134:9000/hbase</value>
</property>
<property>
   <name>hbase.zookeeper.quorum</name>
   <value>192.168.235.134</value>
</property>

THE PC-SIDE JAVA CODE IS AS FOLLOWS.

HBaseAdmin.checkHBaseAvailable (HBaseConfiguration.create ());

The following error:

Org.apache.hadoop.hbase.MasterNotRunningException: com.google.protobuf.ServiceException: java.net.ConnectException: Connection refused: no further information

Actually, I looked at the source code and found that getting the master address in zookeeper always returns localhost, and the problem should be here.
The source code is as follows.
HConnectionManager.java

private Object makeStubNoRetries() throws IOException, KeeperException, ServiceException {
        ZooKeeperKeepAliveConnection zkw;
        try {
          zkw = getKeepAliveZooKeeperWatcher();
        } catch (IOException e) {
          ExceptionUtil.rethrowIfInterrupt(e);
          throw new ZooKeeperConnectionException("Can't connect to ZooKeeper", e);
        }
        try {
          checkIfBaseNodeAvailable(zkw);

!----here always return localhost-----------------------!
          ServerName sn = MasterAddressTracker.getMasterAddress(zkw);

if (sn == null) {
            String msg = "ZooKeeper available but no active master location found";
            LOG.info(msg);
            throw new MasterNotRunningException(msg);
          }
          if (isDeadServer(sn)) {
            throw new MasterNotRunningException(sn + " is dead.");
          }
           Use the security info interface name as our stub key
          String key = getStubKey(getServiceName(), sn.getHostAndPort());
          connectionLock.putIfAbsent(key, key);
          Object stub = null;
          synchronized (connectionLock.get(key)) {
            stub = stubs.get(key);
            if (stub == null) {
              BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sn,
                user, rpcTimeout);
              stub = makeStub(channel);
              isMasterRunning();
              stubs.put(key, stub);
            }
          }
          return stub;
        } finally {
          zkw.close();
        }
      }

I am a Chinese and my English is not good, please forgive me.

Solution

First add IP 192.168.235.134 to the hosts file of Win7
Check whether port 2181 is enabled for 192.168.235.134, or whether it is not enabled.

To open a port in Ubuntu, check it out
https://askubuntu.com/questions/293356/how-to-open-a-particular-port-in-ubuntu

Related Problems and Solutions