1

I am new to java and hadoop. I was practicing mapreduce wordcount example where I came across 2 way of splitting the line in mapper class.

1st one

public class WordCountMapper extends
                Mapper<LongWritable, Text, Text, IntWritable> {
@Override
public void map(LongWritable key, Text value, Context output)
          throws IOException, InterruptedException {
String line = value.toString();
String[] splits = line.split(" ");

 for (String split : splits) {
 output.write(new Text(split), new IntWritable(1));
}
}
}

2nd one

public class WordCountMapper extends
Mapper<LongWritable, Text, Text, IntWritable> {

private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

     public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
     {
         String line = value.toString();
         StringTokenizer tokenizer = new StringTokenizer(line);

         while (tokenizer.hasMoreTokens())
         {
            word.set(tokenizer.nextToken());
            context.write(word, one);
         }
     }
 }

Driver class

public class WordCountDriver 
{
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException 
    {
        Configuration conf = new Configuration();
        Job job = new Job(conf, "WordCount");
        job.setJarByClass(WordCountDriver.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.waitForCompletion(true);
    }
}

when I am using 1st one as mapper class I am getting proper output and if I change only mapper class to 2nd code and Keeping the rest same I get an error as follow

Exception in thread "main" java.lang.NoClassDefFoundError:

I understood that some changes should done in Driver class but my concern is to know what is causing this error!! This may help me understand concepts of java and Hadoop.

Vidya
  • 154
  • 1
  • 17

0 Answers0