列出HBase表的程序给出NoClassDefFoundError

时间:2017-08-28 13:46:45

标签: java hadoop hbase

这是列出HBase表的代码....

package SunHbaseSample.HbaseApp;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class ListTables {
    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
        // Instantiating a configuration class
        Configuration conf = HBaseConfiguration.create();
        // Instantiating HBaseAdmin class
        HBaseAdmin admin = new HBaseAdmin(conf);
        // Getting all the list of tables using HBaseAdmin object
        HTableDescriptor[] tableDescriptor = admin.listTables();
        // printing all the table names.
        for (int i = 0; i < tableDescriptor.length; i++) {
            System.out.println(tableDescriptor[i].getNameAsString());
        }
    }
}

我已经使用maven build创建了一个jar文件,并尝试运行它,如下所示。我正在收到错误,因为主人没有跑

[kumarsu@dhrslnl001 ~]$ hadoop jar HbaseApp-0.0.1-SNAPSHOT.jar SunHbaseSample.HbaseApp.ListTables
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/MasterNotRunningException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:215)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.MasterNotRunningException
    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

1 个答案:

答案 0 :(得分:0)

环境无法找到包含HBase库类的jar。 org.apache.hadoop.hbase.MasterNotRunningException<HBASE_HOME>/lib/hbase-client.jar jar的一部分,因此您需要在类路径中添加它,如下所示。

export HADOOP_CLASSPATH=<HBASE_HOME>/lib/hbase-client.jar
example. 
export HADOOP_CLASSPATH=/usr/hdp/2.3.6.0-3796/hbase/lib/hbase-client.jar

导出HADDOP_CLASSPATH后,hadoop命令会在类路径中添加$HADOOP_CLASSPATH