gpt4 book ai didi

hadoop - map 减少字数统计程序给出 “class not found exception”

转载 作者:行者123 更新时间:2023-12-02 20:49:13 24 4
gpt4 key购买 nike

我的hadoop版本是: 2.8.1

我正在尝试运行Apache Hadoop 2.8.0中的mapreduce示例

WordCount源代码如下(与Apache Hadoop 2.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”。我可以做些什么来创建该类吗?在本教程中,他们没有提到创建该文件的任何其他步骤。

谢谢。

最佳答案

@Yash,能否请您验证命令。我认为您在运行Hadoop jar命令时用小写字母写了类名。

hadoop jar /sharedFiles/wc.jar WordCount /sharedFiles/history  /sharedFiles /output

我希望这能帮到您。

关于hadoop - map 减少字数统计程序给出 “class not found exception”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46629333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com