Remote connect to HBase on Hortonworks sandbox with Java

Spent an entire day trying to remote connect to HBase with Java and finally got it working! It’s actually very simple… Such is life.


OS X 10.9.4

Oracle VirtualBox 4.3.20

Hortonworks Sandbox 2.1 (HBase 0.96.2 and Hadoop 2.2)

Java Code:


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;


public class HBaseConnectionWithJava {
	public static void main(String[] args) throws ServiceException, IOException {

		System.out.println("Trying to connect...");

		Configuration configuration = HBaseConfiguration.create();
		configuration.set("", "2181");
		configuration.set("hbase.zookeeper.quorum", "hortonworks.hbase.vm");
		configuration.set("zookeeper.znode.parent", "/hbase-unsecure");

		System.out.println("HBase is running!");

		// creating a new table
		HTable table = new HTable(configuration, "test");
		System.out.println("Table obtained ");
		// Do whatever you want

What happened
I had the NAT network for my Hortonworks sandbox in VirtualBox, and also a bridged network.

If I set in my machine’s host(I use gas mask) hortonworks.hbase.vm

Then when I run the code is just hangs.

The hbase shell queries works in the VM so I knew HBase was running. If I export the project as a runnable jar and scp the jar into the VM, it runs perfectly in the VM. So it’s definitely network issue for me. I tried everything I could find online, modify `/etc/hosts`, set hosts on my machine, edit the datadir in `zoo.cfg` etc. Did not work.

My co-worker found that what could lead to this is that Zookeeper tries to access localhost and because the VM is on the same machine it confuses it. I’m not sure why but I decided to separate the VM from my local machine and it worked!

I did this through editing VirtualBox’s Network settings. So there’s no need to modify any configuration files inside the Hortonworks sandbox VM such as the `/etc/hosts` file or the `zoo.cfg` file.

In VirtualBox, for the Hortonworks Sandbox VM:
1. Stop the VM if it’s running
2. Go to Settings -> Network, disable all current network adapters.
3. Create a new Adapter, select `Host-only Adapter`, use the default settings and apply it.
4. Start VM, ssh into it with the IP of your VM root@192.168.xx.xx (Mine is which is the IP of the host-only adapter of the VM. While the VM is starting you’ll see that in the console output of the VM), start hbase with command line.

su -l hbase -c "/usr/lib/hbase/bin/ --config /etc/hbase/conf start master"
su -l hbase -c "/usr/lib/hbase/bin/ --config /etc/hbase/conf start regionserver"

5. In the machine’s hosts, add hortonworks.hbase.vm

Note that you can name `hortonworks.hbase.vm` anything you want, just be consistent.

Life is good again!



One thought on “Remote connect to HBase on Hortonworks sandbox with Java

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s