akhil katpally akhil katpally - 2 months ago 31
Java Question

Hadoop Java Error : Exception in thread "main" java.lang.ClassNotFoundException: Titanic

I am trying to run a simple MapReduce program which calculates the average age of male and female. When i am trying to execute it, it is giving me Class Not Found Exception (Titanic class). I have found many questions which has provided the similar answer and based on that i have modified my program but still it's giving me same error. It would be really helpfull if anyone can debug it.

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);
}


}

below are the commands i have executed on it.

creating a jar file:

jar cf example/titanic/titanic.jar example/titanic/Titanic*.class


executing a jar file:

bin/hadoop jar example/titanic/titanic.jar Titanic /user/akhil/titanic/input/TitanicData.txt /user/akhil/titanic/output/

Answer

The jar has is messed up. If your classes belong to the default package, they should not be located under example/titanic/ directories but rather under root directory.