Community Articles

Find and share helpful community-sourced technical articles.
Labels (1)
avatar

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.

26,705 Views
Comments
avatar
Contributor

Hi, Very useful article. I tried this exact code snippet. But, failing. Any help will be appreciated :

I am no HDP 2.4.

Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithoutRetries(RpcRetryingCaller.java:208) at org.apache.hadoop.hbase.client.ClientScanner.call(ClientScanner.java:320) at org.apache.hadoop.hbase.client.ClientScanner.nextScanner(ClientScanner.java:295) at org.apache.hadoop.hbase.client.ClientScanner.initializeScannerInConstruction(ClientScanner.java:160) at org.apache.hadoop.hbase.client.ClientScanner.<init>(ClientScanner.java:155) at org.apache.hadoop.hbase.client.HTable.getScanner(HTable.java:821) 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:304) at com.plantronics.data.storm.hbasetest.ConnectHBase.main(ConnectHBase.java:51) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: java.lang.NullPointerException at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.getMetaReplicaNodes(ZooKeeperWatcher.java:399) at org.apache.hadoop.hbase.zookeeper.MetaTableLocator.blockUntilAvailable(MetaTableLocator.java:552) at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getMetaRegionLocation(ZooKeeperRegistry.java:61) at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateMeta(ConnectionManager.java:1191) at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion(ConnectionManager.java:1158) at org.apache.hadoop.hbase.client.RpcRetryingCallerWithReadReplicas.getRegionLocations(RpcRetryingCallerWithReadReplicas.java:300) at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas.call(ScannerCallableWithReplicas.java:151) at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas.call(ScannerCallableWithReplicas.java:59) at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithoutRetries(RpcRetryingCaller.java:200) ... 14 more

avatar
New Contributor

This is very helpful article but I'm getting error when I tared to create the table into HBase as per your all above mentioned steps and dependencies.

Pls have a look it.

Is there any more properties to set in HBase-site.xml or something other?

File are attached here.

createtable.txt

errorinwhenrunsamecode.txt

avatar
New Contributor

sorry to mention, I have have updated my ipAddress in conf.set("hbase.zookeeper.quorum", "ipAddress");

avatar
Explorer

@Vladimir Zlatkin form where we get these dependencies in jar files?