简体   繁体   中英

Cannot connect to HBase using Java

I am trying to use Java to connect HBase. There are only 1 node, which is my own machine. It seems that I cannot connect successfully.

Here is my Java code:

public class Test {
  public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException, ServiceException {        
    SparkConf conf = new SparkConf().setAppName("Test").setMaster("spark://10.239.58.111:7077");
    JavaSparkContext sc = new JavaSparkContext(conf);
    sc.addJar("/home/cloudera/workspace/Test/target/Test-0.0.1-SNAPSHOT.jar");
    Configuration hbaseConf = HBaseConfiguration.create();
    hbaseConf.addResource(new Path("/usr/lib/hbase/conf/hbase-site.xml"));
    HTable table = new HTable(hbaseConf, "rdga_by_id");
  }
}

I tried to set environment in the codes like this,

hbaseConf.set("hbase.master", "localhost");
hbaseConf.set("hbase.master.port", "60000");
hbaseConf.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConf.set("hbase.zookeeper.quorum", "quickstart.cloudera");
hbaseConf.set("hbase.zookeeper.quorum", "localhost"); 

but still doesn't work.

Here is the hbase-site.xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
  <property>
    <name>hbase.rest.port</name>
    <value>8070</value>
    <description>The port for the HBase REST server.</description>
  </property>
  <property>
  <name>hbase.cluster.distributed</name>
    <value>true</value>
  </property>
  <property>
    <name>hbase.rootdir</name>
    <value>hdfs://quickstart.cloudera:8020/hbase</value>
  </property>
  <property>
    <name>hbase.regionserver.ipc.address</name>
    <value>0.0.0.0</value>
  </property>
  <property>
    <name>hbase.master.ipc.address</name>
    <value>0.0.0.0</value>
  </property>
  <property>
    <name>hbase.thrift.info.bindAddress</name>
    <value>0.0.0.0</value>
  </property>
</configuration>

And in the web UI page while server running, it says serverName is "quickstart.cloudera,16201,1422941563375".

The error goes that

2015-02-02 22:17:03,121 INFO  [main] zookeeper.ZooKeeper (ZooKeeper.java:<init>(438)) - Initiating client connection, connectString=quickstart.cloudera:16201 sessionTimeout=90000 watcher=hconnection-0x62ad0636, quorum=quickstart.cloudera:16201, baseZNode=/hbase
Exception in thread "main" java.io.IOException: java.lang.reflect.InvocationTargetException
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:413)
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:390)
at org.apache.hadoop.hbase.client.HConnectionManager.getConnection(HConnectionManager.java:271)
at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:198)
at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:160)
at Test.main(Test.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.spark.deploy.SparkSubmit$.launch(SparkSubmit.scala:358)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:75)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
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.HConnectionManager.createConnection(HConnectionManager.java:411)
... 12 more
Caused by: java.lang.NoClassDefFoundError: org/cloudera/htrace/Trace
at org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper.exists(RecoverableZooKeeper.java:216)
at org.apache.hadoop.hbase.zookeeper.ZKUtil.checkExists(ZKUtil.java:479)
at org.apache.hadoop.hbase.zookeeper.ZKClusterId.readClusterIdZNode(ZKClusterId.java:65)
at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getClusterId(ZooKeeperRegistry.java:83)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.retrieveClusterId(HConnectionManager.java:839)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.<init>(HConnectionManager.java:642)
... 17 more
Caused by: java.lang.ClassNotFoundException: org.cloudera.htrace.Trace
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)
... 23 more

Sorry to let you guys read so many codes. Thanks in advance

Caused by:

java.lang.NoClassDefFoundError: org/cloudera/htrace/Trace

Based on this line in error stack trace including htrace-core.jar might help.

For Spark-HBase integration, the best way is to add HBase libraries to Spark Classpath. This can be done using the 'compute-classpath.sh' script in $SPARK_HOME/bin folder. Spark invokes 'compute-classpath.sh' and picks up the required hbase jars hence.

export CLASSPATH=$CLASSPATH:<path/to/HBase/lib/*>

eg: export CLASSPATH=$CLASSPATH:/opt/cloudera/parcels/CDH/lib/hbase/lib/*

After this, restart Spark.

There you go :)

提供此罐子的完整路径,如下所示->

sc.addJar("htrace-core.jar");

I got this error when connecting to Hbase from java library without spark as well. I have added below classpath but it didn't work

export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/usr/lib/hbase/*

But then i added hbase-solr path as htrace jar is present inside this path

export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/usr/lib/hbase/*:/usr/lib/hbase-solr/lib/*

Hope this should work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM