How to connect to HBase 1.1 using Java
I had a hard time finding sample code online that uses the latest HBase 1.1+ Java APIs to connect to HDP 2.3 Sandbox. Here is my version for those that already know the Java basics.
public static void main(String[] args) throws IOException {
TableName tableName = TableName.valueOf("stock-prices");
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "sandbox.hortonworks.com");
conf.set("zookeeper.znode.parent", "/hbase-unsecure");
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
if (!admin.tableExists(tableName)) {
admin.createTable(new HTableDescriptor(tableName).addFamily(new HColumnDescriptor("cf")));
}
Table table = conn.getTable(tableName);
Put p = new Put(Bytes.toBytes("AAPL10232015"));
p.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("close"), Bytes.toBytes(119));
table.put(p);
Result r = table.get(new Get(Bytes.toBytes("AAPL10232015")));
System.out.println(r);
}
Add these dependencies through Maven of Gradle:
org.apache.hbase:hbase-client:1.1.1
org.apache.phoenix:phoenix-core:4.4.0-HBase-1.1
Then set hbase.regionserver.ipc.address=0.0.0.0 through Ambari if your Sandbox runs in a VM with multiple NICs.

