【问题标题】:Error while trying to begin a job in Hadoop尝试在 Hadoop 中开始工作时出错
【发布时间】:2013-02-08 00:20:27
【问题描述】:

我一直在尝试在 hadoop 中使用 pagerank 算法,但在作业初始化方面遇到了一些问题。

当我尝试使用 Job 类进行初始化时,编译时出现以下错误:

线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/commons/logging/LogFactory 在 org.apache.hadoop.mapreduce.Job.(Job.java:89) 在 Pagerank.main(Pagerank.java:244)

代码如下:

Job job;
job = new Job();
job.setJarByClass(Pagerank.class);      // In what class are our map/reduce functions for this job found?
job.setMapperClass(PRMap.class);        // What is our map function for this job?
job.setReducerClass(PRReduce.class);    // What is our reduce function for this job?

job.setOutputKeyClass(Text.class);              // What are the (hadoop.io compliant) datatype for our
job.setOutputValueClass(Text.class);            // reducer output's key-value pairs?
job.setInputFormatClass(TextInputFormat.class);     // How will the mapper distinguish (key value) record inputs?
FileInputFormat.addInputPath(job, new Path(args[0])); // First command line argument
FileOutputFormat.setOutputPath(job, new Path("temp0"));
job.waitForCompletion(true);

当我尝试使用 JobConf 类进行初始化时,我收到一个关于所使用的某些方法的争论的错误。

代码如下:

     JobConf conf = new JobConf(Pagerank.class);
     conf.setJobName("pagerank");

     conf.setOutputKeyClass(Text.class);
     conf.setOutputValueClass(Text.class);

     conf.setMapperClass(PRMap.class);
     conf.setReducerClass(PRReduce.class);

     conf.setInputFormat(TextInputFormat.class);
     conf.setOutputFormat(TextOutputFormat.class);

     FileInputFormat.setInputPaths(conf, new Path(args[0]));
     FileOutputFormat.setOutputPath(conf, new Path(args[1]));

     JobClient.runJob(conf);

根据错误:

JobConf 类中的 setMapperClass 方法不能应用于给定类型;

必需:类?扩展映射器

找到:类 PRMap

原因:实参 Class PRMap 无法转换为 Class ?extends Mapper 通过方法调用转换

似乎我无法在 setMapperClass 中将 PRMap.class 作为参数传递,即使我编写的 PRMap 类遵循 Hadoop 的 Map 函数标准

public static class PRMap extends Mapper<LongWritable, Text, Text, Text>
{ ... }

对这两种方法有什么建议吗?

【问题讨论】:

    标签: java configuration hadoop


    【解决方案1】:

    尝试将包含org.apache.commons.Logging.LogFactory jar的jar放到每台机器HadoopHome的Lib目录下,然后重启集群。

    或者您可以尝试使用 libjars 选项通过命令行添加 jar。 如:

    hadoop jar myjar.jar package.classname -libjars mypath/common-loggings.jar

    【讨论】:

      【解决方案2】:

      在您的主方法中添加这一行。

      DistributedCache.addFileToClassPath(new Path("<Absolute Path>/common-loggings.jar"), conf);
      

      【讨论】:

        【解决方案3】:

        看起来 PRMap 类扩展了 org.apache.hadoop.mapreduce.Mapper http://hadoop.apache.org/docs/mapreduce/current/api/org/apache/hadoop/mapreduce/Mapper.html 并且需要通过 JobConf 传递的类应该是 org.apache.hadoop.mapred.Mapper 的子类。

        要解决 java.lang.NoClassDefFoundError 的问题,请将 commons-logging-x.x.x.jar 添加到您的类路径中。

        运行 hadoop 类路径以确认您是否看到 jar 出现。

        【讨论】:

          【解决方案4】:

          这是因为 Mapper 无法找到 LogFactory ,它是 common-loggings.jar 的一部分。为此,您必须让每个客户端映射器都可以访问它,通过将 jar 复制到所有机器或其他有效的方式是复制到分布式缓存中。

          $bin/hadoop fs -copyFromLocal mylib.jar /myapp/mylib.jar
          And accessing it from you code
          DistributedCache.addFileToClassPath(new Path("/myapp/mylib.jar"), job);
          

          更多内容请关注here

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-01-17
            • 2014-05-06
            • 2019-07-19
            • 2012-10-11
            • 2012-01-13
            • 1970-01-01
            • 2019-03-16
            • 2011-11-07
            相关资源
            最近更新 更多