Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Unable to Connect Hbase from Java client in Eclipse

Unable to Connect Hbase from Java client in Eclipse

New Contributor

Hi 

I am using cloudera quick start VM. my Hbase is working fine and using Hbase shell I am able to create table and do CRUD operations. I would like to connect Hbase with Java in Eclipse. I already created a Maven project in Eclipse and all the required Jar files downloaded properly. I written below code.

package hbase;
import java.io.IOException;

import com.google.protobuf.ServiceException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.ConnectionFactory;
public class GetConnection {

public static void main(String[] args) throws Exception {

Configuration config = HBaseConfiguration.create();

//String path = this.getClass().getClassLoader().getResource("hbase-site.xml").getPath();

// config.addResource(new Path(path));
config.addResource("usr/lib/hbase/conf/hbase-site.xml");
config.set("hbase.zookeeper.quorum","localhost");
config.set("hbase.zookeeper.property.client.port","2181");
config.setInt("timeout", 120000);
config.set("hbase.master", "localhost:60000");
Connection con = ConnectionFactory.createConnection(config);
HBaseAdmin admin = new HBaseAdmin(config);

try {
HBaseAdmin.checkHBaseAvailable(config);
}
catch (MasterNotRunningException e)
{
System.out.println("HBase is not running." + e.getMessage());
return;
}

}
}

But Once I executed this code I am getting below error


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.
Exception in thread "main" java.io.IOException: java.lang.reflect.InvocationTargetException
at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java:240)
at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java:218)
at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java:119)
at hbase.GetConnection.main(GetConnection.java:32)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java:238)
... 3 more
Caused by: java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/types/CopyOnWriteArrayMap
at org.apache.hadoop.hbase.client.MetaCache.<init>(MetaCache.java:52)
at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.<init>(ConnectionManager.java:714)
at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.<init>(ConnectionManager.java:643)
... 8 more
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.types.CopyOnWriteArrayMap
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 11 more

Please let me know What is the Wrong . Thanks in advance

Regards

Rajesh 

 

 

 

 

 

 

 

4 REPLIES 4
Highlighted

Re: Unable to Connect Hbase from Java client in Eclipse

Super Mentor

@rajesh1972 

Can you please share your  "pom.xml" file so that we can check the dependencies which you are including here?

 

Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.types.CopyOnWriteArrayMap

 

The problematic/missing class "org.apache.hadoop.hbase.types.CopyOnWriteArrayMap" is part of "hbase-common.jar" JAR so please make sure that your "pom.xml" file contains the dependency to that maven artifact as well.  The "hbase-server" artifact provides the dependency to hbase-common jar.

Example:

 

    <dependencies>
        <dependency>
        	<groupId>org.apache.hbase</groupId>
        	<artifactId>hbase-server</artifactId>
        	<version>${hbase.version}</version>
        </dependency>
        <dependency>
        	<groupId>org.apache.hbase</groupId>
        	<artifactId>hbase-client</artifactId>
        	<version>${hbase.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>${hadoop.version}</version>
        </dependency>                
    </dependencies>

 

 

Please Notice:  

 

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>${hbase.version}</version>
</dependency>

(OR)

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-common</artifactId>
    <version>${hbase.version}</version>
</dependency>

 

 

 

You can refer to a working pom.xml in my Github repo:

https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/HDP_Ambari/HBase/HBase_Client

 

hbase-server maven artifact provides the dependency to hbase-common so please include the dependency 

 

# mvn dependency:tree | grep -B3 hbase-common

[INFO] +- org.apache.hbase:hbase-server:jar:1.1.2.2.5.3.0-37:compile
[INFO] |  +- org.apache.hbase:hbase-common:jar:1.1.2.2.5.3.0-37:compile

 

 

.

 

.

 

 

 

Re: Unable to Connect Hbase from Java client in Eclipse

New Contributor

Here is my POM.XML

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nosql</groupId>
<artifactId>gettingstarted</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com</groupId>
<artifactId>hbase</artifactId>
<version>0.0.1-HBASE-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>

</dependency>

<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.1.2</version>

</dependency>

Re: Unable to Connect Hbase from Java client in Eclipse

New Contributor

Here is correct POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>nosql</groupId>
<artifactId>gettingstarted</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com</groupId>
<artifactId>hbase</artifactId>
<version>0.0.1-HBASE-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0</version>

</dependency>




<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.1.2</version>

</dependency>




</dependencies>
</project>

Re: Unable to Connect Hbase from Java client in Eclipse

Super Mentor

@rajesh1972 

The maven artifact : "hbase-common:1.1.2" which provides the following JAR does. not seems to contain that class "org.apache.hadoop.hbase.types.CopyOnWriteArrayMap" ... you can check this JAR ..
https://repo1.maven.org/maven2/org/apache/hbase/hbase-common/1.1.2/hbase-common-1.1.2.jar

 


So can you please try changing the version of the artifact to 2.0.0 . (instead of 1.1.2) and then see if that works for you?

Example:

 

   <dependencies>
      <dependency>
         <groupId>org.apache.hbase</groupId>
         <artifactId>hbase-client</artifactId>
         <version>2.0.0</version>
      </dependency>
      <dependency>
         <groupId>org.apache.hbase</groupId>
         <artifactId>hbase-common</artifactId>
         <version>2.0.0</version>
      </dependency>
   </dependencies>

 

.

 

If your question is answered then, Please make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.

 

 

.