【发布时间】:2021-05-28 05:07:55
【问题描述】:
我正在尝试学习如何使用 Hadoop 1.2.1。
我已经创建了我的第一个集群,但无法在 java 中编译 mapreduce 示例(Eclipse 2021-03)
这是我的源代码:
import java.io.*;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapred.Reducer;
public class WordCount {
public static class WordCountMapper extends MapReduceBase
implements Mapper<LongWritable, Text, Text, IntWritable> {
private Text word = new Text();
public void map(LongWritable key, Text value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, new IntWritable(1));
}
}
}
public static class WordCountReducer extends MapReduceBase
implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
int total = 0;
while (values.hasNext()) {
total += values.next().get();
}
output.collect(key, new IntWritable(total));
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("Word Count");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(WordCountMapper.class);
conf.setReducerClass(WordCountReducer.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.addInputPath(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
我不是此代码的作者,只是在网络某处找到示例。
While trying to run application, it shows me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/mapred/JobConf
at WordCount.main(WordCount.java:45)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.mapred.JobConf
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
找不到 JobConf 类的原因是什么? 在引用的库中,我有 hadoop-core-1.2.1.jar 和 hadoop 目录中的其他库。
谁能告诉我我做错了什么? :)
最好的问候!
【问题讨论】:
-
你需要在你的项目中添加一些库。在 Eclipse 中创建一个可执行的 Jar。在Java中检查这个错误
NoClassDefFoundError:,你可以找到很多解决方案。