hadoop jar 找不到main class_10年老架构,教你HadoopJob使用第三方依赖jar文件,不来就后悔吧...

本文详细介绍了在Hadoop中运行MapReduce Job时如何处理找不到main class的问题。通过设置HADOOP_CLASSPATH环境变量和使用-libjars选项,或者通过Maven将依赖打包进jar文件,来确保Job正确运行。同时提供了具体的命令行示例和Maven插件配置。
摘要由CSDN通过智能技术生成

前言

当我们实现了一个Hadoop MapReduce Job以后,而这个Job可能又依赖很多外部的jar文件,在Hadoop集群上运行时,有时会出现找不到具体Class的异常。出现这种问题,基本上就是在Hadoop Job执行过程中,没有从执行的上下文中找到对应的jar文件(实际是unjar的目录,目录里面是对应的Class文件)。所以,我们自然而然想到,正确配置好对应的classpath,MapReduce Job运行时就能够找到。有两种方式可以更好地实现,一种是设置HADOOP_CLASSPATH,将Job所依赖的jar文件加载到HADOOP_CLASSPATH,这种配置只针对该Job生效,Job结束之后HADOOP_CLASSPATH会被清理;另一种方式是,直接在构建代码的时候,将依赖jar文件与Job代码打成一个jar文件,这种方式可能会使得最终的jar文件比较大,但是结合一些代码构建工具,如Maven,可以在依赖控制方面保持一个Job一个依赖的构建配置,便于管理。下面,我们分别说明这两种方式。

62f68e57a93d8e140c33b9e5e1274d0a.png

设置HADOOP_CLASSPATH

比如,我们有一个使用HBase的应用,操作HBase数据库中表,肯定需要ZooKeeper,所以对应的jar文件的位置都要设置正确,让运行时Job能够检索并加载。Hadoop实现里面,有个辅助工具类org.apache.hadoop.util.GenericOptionsParser,能够帮助我们加载对应的文件到classpath中,操作比较容易一些。下面我们是我们实现的一个例子,程序执行入口的类,代码如下所示:

package org.shirdrn.kodz.inaction.hbase.job.importing; import java.io.IOException;import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.io.ImmutableBytesWritable;import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.util.GenericOptionsParser; /*** Table DDL: create 't_sub_domains', 'cf_basic', 'cf_status'* 
* cf_basic:domain cf_basic:len* cf_status:status cf_status:live* ** @author shirdrn*/public class DataImporter {      public static void main(String[] args)               throws IOException, InterruptedException, ClassNotFoundException, URISyntaxException {                    Configuration conf = HBaseConfiguration.create();          String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();                    assert(otherArgs.length == 2);                    if(otherArgs.length < 2) {               System.err.println("Usage: " +                         " ImportDataDriver -libjars 
[,...[,]] ");               System.exit(1);          }          String tableName = otherArgs[0].trim();          String input = otherArgs[1].trim();                    // set table columns          conf.set("table.cf.family", "cf_basic");          conf.set("table.cf.qualifier.fqdn", "domain");          conf.set("table.cf.qualifier.timestamp", "create_at");                              Job job = new Job(conf, "Import into HBase table");          job.setJarByClass(DataImporter.class);          job.setMapperClass(ImportFileLinesMapper.class);          job.setOutputFormatClass(TableOutputFormat.class);                    job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, tableName);          job.setOutputKeyClass(ImmutableBytesWritable.class);          job.setOutputValueClass(Put.class);                    job.setNumReduceTasks(0);                    FileInputFormat.addInputPath(job, new Path(input));                    int exitCode = job.waitForCompletion(true) ? 0 : 1;          System.exit(exitCode);     } }

可以看到,我们可以通过-libjars选项来指定该Job运行所依赖的第三方jar文件,具体使用方法,说明如下:

  • 第一步:设置环境变量

我们修改.bashrc文件,增加如下配置内容:

export HADOOP_HOME=/opt/stone/cloud/hadoop-1.0.3export PATH=$PATH:$HADOOP_HOME/binexport HBASE_HOME=/opt/stone/cloud/hbase-0.94.1export PATH=$PATH:$HBASE_H
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值