0
package com.reducer.wc;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*;
public class WordCount extends Configured implements Tool {
  public static void main(String args[]) throws Exception {
    int res = ToolRunner.run(new WordCount(), args);
    System.exit(res);
  }
  public int run(String[] args) throws Exception {
    Path inputPath = new Path(args[0]);
    Path outputPath = new Path(args[1]);
    Configuration conf = getConf();
    Job job = new Job(conf, this.getClass().toString());
    FileInputFormat.setInputPaths(job, inputPath);
    FileOutputFormat.setOutputPath(job, outputPath);
    job.setJobName("WordCount");
    job.setJarByClass(WordCount.class);
    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    job.setMapperClass(Map.class);
    job.setCombinerClass(Reduce.class);
    job.setReducerClass(Reduce.class);
    return job.waitForCompletion(true) ? 0 : 1;
  }
  public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
    @Override
    public void map(LongWritable key, Text value,
                    Mapper.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);
      }
    }
  }
  public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    @Override
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable value : values) {
        sum += value.get();
      }
      context.write(key, new IntWritable(sum));
    }
  }
}

I am running this in eclipse it gives following error !

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
    at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:59)
    at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:79)
    at com.reducer.wc.WordCount.main(WordCount.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 3 more

What is the actual reason for this error. Do, I have to make the jar and run it in hadoop to see the output ? or else i can see it in eclipse. I am using a hadoop plugin in eclipse.

Thanks in advance.

Shubhankar
  • 95
  • 1
  • 14
  • http://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java Does not answers my full question. Read carefully! – Shubhankar Jun 25 '16 at 19:06
  • There are quite a few JAR files you need to run Mapreduce. Using Maven would make managing those easier – OneCricketeer Jun 25 '16 at 19:07
  • And its definitely a duplicate as you specifically asked for the reason for the error. – OneCricketeer Jun 25 '16 at 19:08
  • What are those can u tell ? I am using hadoop-core and commons-logging – Shubhankar Jun 25 '16 at 19:08
  • Do, I have to make the jar and run it in hadoop to see the output ? or else i can see it in eclipse. I am using a hadoop plugin in eclipse. This question is not there i hope! – Shubhankar Jun 25 '16 at 19:09
  • Your error says you're missing commons-cli, I'm not sure what else you need at the moment. And yes, you have to make a JAR and run it with `hadoop jar` and specify inputs and outputs that reside on HDFS – OneCricketeer Jun 25 '16 at 19:19

0 Answers0