运行hbase java程序时出错

时间:2017-08-14 09:50:46

标签: maven hadoop mapreduce hbase apache-zookeeper

这是我的java hbase createtable程序,如下所示: -

 public class createtable 
 {

  public static void main(String[] args) throws IOException 

  {   
       Configuration conf = HBaseConfiguration.create();    
       conf.set("hbase.zookeeper.quorum", "sandbox.hortonworks.com");
       conf.set("hbase.zookeeper.property.clientPort", "2181");
       conf.set("zookeeper.znode.parent", "/hbase-unsecure");
       HBaseAdmin admin = new HBaseAdmin(conf);
       HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("people"));
       tableDescriptor.addFamily(new HColumnDescriptor("personal"));
       tableDescriptor.addFamily(new HColumnDescriptor("contactinfo"));
       admin.createTable(tableDescriptor);
       Put put = new Put(Bytes.toBytes("doe-john-m-12345");     
   }

在运行命令后创建程序的jar(table-0.0.1-SNAPSHOT.jar)之后  hadoop jar table-0.0.1-SNAPSHOT.jar table.createtable

我正在

   Exception in thread "main" java.lang.NoClassDefFoundError:org/apachehadoop/hbase/HBaseConfiguration at 
    table.createtable.main(createtable.java:17)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav     a:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:234)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:148)                           Causedby:
     java.lang.ClassNotFoundException:org.apache.hadoop.hbase.HBase
     Configuration     
     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

 how do i resolve this error ??

3 个答案:

答案 0 :(得分:1)

您的代码无法看到 hbase.jar 。尝试在 pom.xml

中添加此部分
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase -->
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase</artifactId>
    <version>0.90.2</version>
</dependency>

然后运行它。

答案 1 :(得分:1)

请阅读此博客:https://my-bigdata-blog.blogspot.com/2017/08/hbase-programming-on-map-reduce-with.html

要使其发挥作用的两点

1)你的代码应该有TableMapReduceUtil.addDependencyJars(job);

2)在命令行上 - 执行命令之前执行以下操作:

导出HADOOP_CLASSPATH =你的jar路径

export HADOOP_CLASSPATH = $ HADOOP_CLASSPATH:hbase classpath

这会添加hbase库以供执行。你在maven / netbeans中添加的那个是用于编译的。

答案 2 :(得分:0)

我解决了这个问题,因为在制作胖jar时存在问题,并且在新的hbase版本jars中被弃用了许多依赖项。所以我还添加了hbase-client-1.2.6-jar和hbase-common-1.2.6 -jar外部蚀,然后建造胖罐...这解决了我的问题。