我的hadoop版本是:2.8.1
我正在尝试运行ApacheHadoop2.8.0中的mapreduce示例
wordcount源代码如下(与apachehadoop2.8.0示例中给出的相同)
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException,
InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
我通过插入上面的代码创建了一个wordcount.java文件。然后我编译了它。
javac -cp $HADOOP_CLASSPATH /sharedFiles/WordCount.java
然后我结合wordcount*.classes创建一个wc.jar文件。
jar cf /sharedFiles/wc.jar /sharedFiles/WordCount*.class
在sharedfiles文件夹中,文件如下所示。
ls /sharedFiles
history wc.jar WordCount.class
WordCount$IntSumReducer.class
WordCount.java WordCount$TokenizerMapper.class
然后我试着运行mapreduce命令。
hadoop jar /sharedFiles/wc.jar wordcount /sharedFiles/history /sharedFiles /output
它让我犯了这个错误。
Exception in thread "main" java.lang.ClassNotFoundException: wordcount
我注意到驱动程序类“wordcount”并没有在相应的文件夹中创建。我能做些什么来创建那个类吗?在教程中,他们没有提到创建该文件的任何附加步骤。
谢谢您。
1条答案
按热度按时间mzmfm0qo1#
@雅什,你能核实一下你的命令吗。我认为您在运行hadoopjar命令时用小写字母编写了类名。
我希望这对你有帮助。