Hbase Java API连接错误

时间:2016-04-18 11:10:19

标签: java hadoop hbase hortonworks-data-platform

我尝试从Java应用程序连接到Hbase,如下所示:

Configuration config = HBaseConfiguration.create();
    config.set("hbase.zookeeper.quorum", "localhost");
    config.set("hbase.zookeeper.property.clientPort", "2181");
    config.set("zookeeper.znode.parent", "/hbase-unsecure");

    config.set("hbase.client.retries.number", Integer.toString(0));
    config.set("zookeeper.session.timeout", Integer.toString(60000));
    config.set("zookeeper.recovery.retry", Integer.toString(0));

    Connection conn = ConnectionFactory.createConnection(config);
    TableName TABLE_NAME = TableName.valueOf("weblog");
    Table table = conn.getTable(TABLE_NAME);
    Result r = table.get(new Get(Bytes.toBytes("row1")));
    System.out.println(r);

我将应用程序构建到JAR中,但在群集上运行时使用:

hadoop jar hbaseConnect-0.0.1-SNAPSHOT.jar com.packagename.hbaseConnect.HbaseConnect

我收到以下错误:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration
    at com.DigiMerket.hbaseConnect.HbaseConnect.main(HbaseConnect.java:23)
    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 org.apache.hadoop.util.RunJar.run(RunJar.java:221)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.HBaseConfiguration
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

我尝试将HBASE_CLASSPATH添加到hadoop-env中的HADOOP_CLASSPATH,就像这个post建议但是我得到了同样的错误..

2 个答案:

答案 0 :(得分:0)

通过设置" HADOOP_CLASSPATH"解决问题如下所示,然后在同一个终端运行hadoop / hbase jar:

 export HADOOP_CLASSPATH="$HADOOP_CLASSPATH:/usr/hdp/2.3.4.0-3485/hbase/lib/hbase-client.jar:/usr/hdp/2.3.4.0-3485/hbase/lib/hbase-common.jar:/usr/hdp/2.3.4.0-3485/hbase/lib/hbase-protocol-1.1.2.2.3.4.0-3485.jar:.:" 

运行jar

hadoop  jar  hbaseConnect-0.0.1-SNAPSHOT.jar com.packagename.hbaseConnect.HbaseConnect

答案 1 :(得分:0)

日志提示您运行jar需要依赖其他jar。 比如hbase-client.jar

像libs一样mkdir并将依赖jar放入libs的目录中 并使用此命令: java -Djava.ext.dirs = / xxx / xxx / xxx / lib -jar / xxx / xxx / you's jar

希望这可以帮到你。