Support Questions

Find answers, ask questions, and share your expertise

Unable to use Hbase from Java API

avatar

I am using HDP 2.5 Single Node. And I am using OSX. I have include hbase-site.xml in my local path.

The following is my java code in local. I am running it from eclipse. I create a table called as demo using the shell

package hbase;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;

public class Sample {
	public static void main(String[] args) throws IOException{
	       System.out.println("Trying to connect...");
	       Configuration conf = HBaseConfiguration.create();
	       System.out.println("HBase is running!");
	

               Connection conn = ConnectionFactory.createConnection(conf);
               Table  table = conn.getTable(TableName.valueOf("demo"));
               Scan scan1 = new Scan();
            
              ResultScanner scanner1 = table.getScanner(scan1);

             for(Result res: scanner1){

        	   System.out.print(res);

             }
           System.out.println("Table created Successfully...");
	}
}




And I get the following output

Trying to connect...

HBase is running!

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

2017-03-22 22:07:10.625 java[30947:3719243] Unable to load realm mapping info from SCDynamicStore

Exception in thread "main" java.lang.RuntimeException: 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 hbase.Sample.main(Sample.java:29)

Caused by: java.lang.RuntimeException: java.lang.NullPointerException

	at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithoutRetries(RpcRetryingCaller.java:208)

	at org.apache.hadoop.hbase.client.ClientSmallReversedScanner.loadCache(ClientSmallReversedScanner.java:211)

	at org.apache.hadoop.hbase.client.ClientSmallReversedScanner.next(ClientSmallReversedScanner.java:185)

	at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegionInMeta(ConnectionManager.java:1249)

	at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion(ConnectionManager.java:1155)

	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)

	... 6 more

Caused by: java.lang.NullPointerException

	at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.getMetaReplicaNodes(ZooKeeperWatcher.java:395)

	at org.apache.hadoop.hbase.zookeeper.MetaTableLocator.blockUntilAvailable(MetaTableLocator.java:553)

	at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getMetaRegionLocation(ZooKeeperRegistry.java:61)

	at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateMeta(ConnectionManager.java:1185)

	at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion(ConnectionManager.java:1152)

	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

Please tell me what am I doing wrong.

7 REPLIES 7

avatar
Master Mentor

@Aditya Sharma

Please define the "zookeeper.znode.parent" property in your code something like following:

Configuration conf = HBaseConfiguration.create();
conf.set("zookeeper.znode.parent", "/hbase-unsecure");

.

Parent value may differ in your case though but you can find the value of "zookeeper.znode.parent" in the "hbase-site.xml" file.

avatar

@Jay SenSharma

If I use it my code gets stuck with the following output.

Trying to connect...

HBase is running!

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

2017-03-22 22:57:48.776 java[31831:3869652] Unable to load realm mapping info from SCDynamicStore

avatar
Master Mentor

@Aditya Sharma

Is it a kerberized environment? If yes then have you set on the client side.

export HADOOP_OPTS="-Djava.security.krb5.realm=YOUR.REALM.NAME -Djava.security.krb5.kdc=my.kdc.com

.

Also Can you pelas emake sure that "hbase-site.xml" file is present in clients classpath and your code knows the Zookeeper Addresses and those host and ports are accessible from your machine where you are running the java code.

Example:

      config.set("hbase.zookeeper.quorum", "node1.example.com");
      config.set("hbase.zookeeper.property.clientPort", "2181")

.

avatar

@Jay SenSharma

I did not install kerberized on sandbox. So i guess it is not there.

And I have hbase-site.xml in my classpath. I even tried after adding the two statements but I get the same error. I also made a runnable jar and copied it to the cluster. Still I was getting the same error.

avatar
Master Mentor

@Aditya Sharma

I have developed a very simple HBase Client code completely using maven so it would be easier for you to test and run.

Please refer to the demo: https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/HDP_Ambari/HBase_Client

Few changes you will need to make in the code.

        config.set("hbase.zookeeper.quorum", "erie3.example.com,erie1.example.com,erie4.example.com,erie2.example.com");
        config.set("hbase.zookeeper.property.clientPort", "2181");
        config.set("zookeeper.znode.parent", "/hbase-unsecure");
        config.addResource(new Path("/PATH/TO/HBase_Client/src/main/resources/hbase-site.xml"));
        config.addResource(new Path("/PATH/TO/HBase_Client/src/main/resources/core-site.xml"));
        config.addResource(new Path("/PATH/TO/HBase_Client/src/main/resources/hdfs-site.xml"));

.

Please use your own "hbase-site.xml","core-site.xml" and "hdfs-site.xml" files.

.

Then run it as following:

How to build and run?
# cd /PATH/TO/HBase_Client
# mvn clean install exec:java

avatar
Contributor

Can you add following in your code and see whether that helps?

conf.addResource(<path to hbase site.xml>+"/hbase-site.xml");

avatar

@trupti

Using this my code is getting stuck with the following output

Trying to connect...

HBase is running!

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

2017-03-23 11:44:44.217 java[33698:4112278] Unable to load realm mapping info from SCDynamicStore