Java 尝试在Hadoop中开始作业时出错

Java 尝试在Hadoop中开始作业时出错,java,configuration,hadoop,Java,Configuration,Hadoop,我一直在尝试在hadoop中使用pagerank算法,但在初始化作业时遇到了一些问题 当我尝试使用作业类进行初始化时,在编译时出现以下错误: 线程“main”java.lang.NoClassDefFoundError中出现异常:org/apache/commons/logging/LogFactory 位于org.apache.hadoop.mapreduce.Job.(Job.java:89) 在Pagerank.main(Pagerank.java:244) 代码如下: Job job;

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

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

线程“main”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 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类进行初始化时,我得到了一个关于所使用的一些方法的参数的错误

代码如下:

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 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

原因:实际参数类PRMap无法转换为类?通过方法调用转换扩展映射器

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

public static class PRMap extends Mapper<LongWritable, Text, Text, Text>
{ ... }
公共静态类PRMap扩展映射器
{ ... }

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

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

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

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


看起来PRMap类扩展了org.apache.hadoop.mapreduce.Mapper,需要通过JobConf传递的类应该是org.apache.hadoop.mapred.Mapper的子类

要修复java.lang.NoClassDefFoundError的问题,请将commons-logging-x.x.jar添加到类路径中


运行hadoop类路径以确认是否看到jar显示。

在Main方法中添加这一行

DistributedCache.addFileToClassPath(new Path("<Absolute Path>/common-loggings.jar"), conf);
DistributedCache.addFileToClassPath(新路径(“/common loggings.jar”),conf);

这是因为映射器无法找到
日志工厂
,它是
common loggings.jar的一部分。为此,您必须使每个客户机映射程序都可以访问它,方法是将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);
可以找到更多