3

I am trying to execute the below code

package test;

import java.io.IOException;

import java.util.*;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.conf.*;

import org.apache.hadoop.io.*;

import org.apache.hadoop.util.*;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.conf.Configured;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class Diction {

    public static class WordMapper extends Mapper<Text,Text,Text,Text>
    {
         private Text word = new Text();
            public void map(Text key, Text value, Context context) throws IOException, InterruptedException
            {
                StringTokenizer itr = new StringTokenizer(value.toString(),",");
                while (itr.hasMoreTokens())
                {
                    word.set(itr.nextToken());
                    context.write(key, word);
                }
    }

}
 public static class AllTranslationsReducer

    extends Reducer<Text,Text,Text,Text>
    {
        private Text result = new Text();
        public void reduce(Text key, Iterable<Text> values,
        Context context
        ) throws IOException, InterruptedException
        {
            String translations = "";
            for (Text val : values)
            {
                translations += "|"+val.toString();
            }
            result.set(translations);
            context.write(key, result);
        }
    }
    public static void main(String[] args) throws Exception
    {
        Configuration conf = new Configuration();
        Job job = new Job(conf, "dictionary");
        job.setJarByClass(Dictionary.class);
        job.setMapperClass(WordMapper.class);
        job.setReducerClass(AllTranslationsReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setInputFormatClass(KeyValueTextInputFormat.class);
        FileInputFormat.addInputPath(job, new Path("/tmp/hadoop-cscarioni/dfs/name/file"));
        FileOutputFormat.setOutputPath(job, new Path("output"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

But i am finding some error "The import org.apache.hadoop.mapreduce cannot be resolved" I have been already added Hadoop Jar file "http://www.java2s.com/Code/Jar/h/Downloadhadoop0210eclipsepluginjar.htm" .

Hadoop version-Hadoop 2.0.0-cdh4.2.0

Eclipse-juno Service Release 2 Can any one please help me to resole this issue.

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
Indrajit Swain
  • 1,505
  • 1
  • 15
  • 22

2 Answers2

7

Try including the following external jars that you downloaded with Hadoop:

  • $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.2.0.jar
  • $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-common-2.2.0.jar
  • $HADOOP_HOME/share/hadoop/common/hadoop-common-2.2.0.jar
6

You don't have the appropriate dependency. What you have a actually is an Eclipse plugin for Hadoop development which it is totally different from Hadoop's Jar.

Have a look at the contents of hadoop-0.21.0-eclipse-plugin.jar , do you see any Hadoop core classes?

Check Hadoop Releases and add the real Hadoop dependency to your build tool (Maven, Ant, Gradle ...).

Something like if you are using Maven:

<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-client</artifactId>
  <version>2.2.0</version>
</dependency>
Chiron
  • 20,081
  • 17
  • 81
  • 133
  • Yes in my code when i am getting error in each Import statement "import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;" It is showing me the same error could you please suggest me how can i resolve this . Using this VM previously i am able to execute my codes but currently it is showing errors . – Indrajit Swain Feb 09 '14 at 12:32
  • @IndrajitSwain Yes exactly. Because you don't have Hadoop's jars in your classpath. – Chiron Feb 09 '14 at 12:34
  • Can you please explain me how i can add Hadoop's Jar in my class path ,OS-Linux. – Indrajit Swain Feb 09 '14 at 12:41
  • Yes , In my local machine i am able to find the Hadoop-common folder and it contain all the details, Can you please explain , is there any Jar i need to add through Build Add External Jar – Indrajit Swain Feb 09 '14 at 12:46
  • @IndrajitSwain You need hadoop-client jar. Again, check my answer. And please do yourself a favor, you a build tool. Don't relay on your IDE build capabilities. – Chiron Feb 09 '14 at 12:48
  • Yes,you are correct the jar file i have added it is not contain any core Hadoop core class . :( But i have searched so many jar file and they are not solving out my problem – Indrajit Swain Feb 09 '14 at 12:51
  • 1
    Great my problem now solved, The Jar files i have been added previously is not working properly , I have now added correct Jar file "http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common/2.0.0-alpha" ,"http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core/2.0.2-alpha" – Indrajit Swain Feb 09 '14 at 13:00
  • @IndrajitSwain Then feel free to mark my answer as 'Accepted'! (if it helps you) – Chiron Feb 09 '14 at 13:14