我正在尝试运行一个简单的MapReduce程序来计算男性和女性的平均年龄。当我试图执行它时,它给了我Class Not Found Exception(泰坦尼克号类)。我发现很多问题提供了类似的答案,并基于我修改了我的程序,但它仍然给我同样的错误。如果有人可以调试它,那将非常有用。
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class Titanic{
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable>{
private Text category = new Text();
public void map(LongWritable key, Text text, Context context) throws IOException, InterruptedException{
String line = text.toString();
String str[] = line.split(",");
if(str[4] == "male"){
category.set(str[4]);
}else{
category.set(str[4]);
}
IntWritable value = new IntWritable(Integer.parseInt(str[5]));
context.write(category,value);
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, FloatWritable>{
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
float average = 0;
int count =0;
for(IntWritable val : values){
average = average+val.get();
count = count + 1;
}
average =average/count;
context.write(key, new FloatWritable(average));
}
}
public static void main(String[] args) throws Exception{
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "titanic");
job.setJarByClass(Titanic.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
以下是我在其上执行的命令。
创建一个jar文件:
jar cf example/titanic/titanic.jar example/titanic/Titanic*.class
执行jar文件:
bin/hadoop jar example/titanic/titanic.jar Titanic /user/akhil/titanic/input/TitanicData.txt /user/akhil/titanic/output/
答案 0 :(得分:0)
删除*:
jar cf example/titanic/titanic.jar example/titanic/Titanic.class
答案 1 :(得分:0)
罐子搞砸了。如果您的类属于默认包,则它们不应位于example/titanic/
目录下,而应位于根目录下。