导入org.apache.hadoop.mapreduce无法解析

时间:2014-02-09 11:53:37

标签: hadoop mapreduce hive hadoop-streaming hadoop-plugins

我正在尝试执行以下代码

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

但我发现一些错误“import org.apache.hadoop.mapreduce无法解决” 我已经添加了Hadoop Jar文件“http://www.java2s.com/Code/Jar/h/Downloadhadoop0210eclipsepluginjar.htm”。

Hadoop版本-Hadoop 2.0.0-cdh4.2.0

Eclipse-juno Service Release 2  任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:6)

您没有适当的依赖关系。实际上你有一个用于Hadoop开发的Eclipse插件,它与Hadoop的Jar完全不同。

看看 hadoop-0.21.0-eclipse-plugin.jar 的内容,你看到任何Hadoop核心类吗?

检查Hadoop Releases并将真正的Hadoop依赖项添加到您的构建工具(Maven,Ant,Gradle ...)。

如果您正在使用Maven,那就是:

<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-client</artifactId>
  <version>2.2.0</version>
</dependency>

答案 1 :(得分:5)

尝试包含您使用Hadoop下载的以下外部jar:

  • $ HADOOP_HOME /共享/的hadoop / MapReduce的/ Hadoop的MapReduce的客户端 - 芯 - 2.2.0.jar
  • $ HADOOP_HOME /共享/的hadoop / MapReduce的/ Hadoop的MapReduce的客户端 - 共2.2.0.jar
  • $ HADOOP_HOME /共享/ hadoop的/普通/ Hadoop的共2.2.0.jar