# Ubuntu使用eclipse运行MapReduce程序

## 1、安装hadoop-eclipse-plugin插件

eclipse运行Mapreduce程序需要一个hadoop-eclipse-plugin-2.7.6.jar 插件。将此插件放到eclipse的安装目录的dropins目录下之后重启eclipse。

## 2、确定Hadoop的安装目录

在windows->PerFerences 下可以看到 Hadoop Map/Reduce,点击Browse选择Hadoop的安装目录

![](D:\Hadoop-Map-Reduce.png)

这个时候在Perspectivce->Open Perspectice->other 下可以看到Map/Reduce选项。

![](D:\Map-Reduce.png)

## 3、创建Map/Reduce项目

创建一个Map/Reduce项目，file->new Project->Map/Reduce Project

![](D:\Map-Reduce-Project.png)

![](D:\目录.png)

## 5、创建一个Hadoop连接

![](D:\new Hadoop.png)

![](D:\edit-hadoopconnect.png)

## 6、连接成功

![](D:\success.png)

## 7、通过 Eclipse 运行 MapReduce

在运行 MapReduce 程序前，还需要执行一项重要操作（也就是上面提到的通过复制配置文件解决参数设置问题）：将 /usr/local/hadoop/etc/hadoop 中将有修改过的配置文件（如伪分布式需要 core-site.xml 和 hdfs-site.xml），以及 log4j.properties 复制到 WordCount 项目下的 src 文件夹（~/workspace/WordCount/src）中：

```shell
cp /usr/local/hadoop/etc/hadoop/core-site.xml /root/eclipse/workspace/java-mapreduce/src
cp /usr/local/hadoop/etc/hadoop/hdfs-site.xml /root/eclipse/workspace/java-mapreduce/src
cp /usr/local/hadoop/etc/hadoop/log4j.properties /root/eclipse/workspace/java-mapreduce/src
```

**workcount.java代码如下**

```shell
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
    public WordCount() {
    }
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        //String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
        String[] otherArgs=new String[]{"input","output"}; /* 直接设置输入参数 */
        if(otherArgs.length < 2) {
            System.err.println("Usage: wordcount <in> [<in>...] <out>");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCount.TokenizerMapper.class);
        job.setCombinerClass(WordCount.IntSumReducer.class);
        job.setReducerClass(WordCount.IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        for(int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
        System.exit(job.waitForCompletion(true)?0:1);
    }
    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();
        public IntSumReducer() {
        }
        public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum = 0;
            IntWritable val;
            for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
                val = (IntWritable)i$.next();
            }
            this.result.set(sum);
            context.write(key, this.result);
        }
    }
    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public TokenizerMapper() {
        }
        public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while(itr.hasMoreTokens()) {
                this.word.set(itr.nextToken());
                context.write(this.word, one);
            }
        }
    }
}
```

**导入Jar**

- 1、/usr/local/hadoop/share/hadoop/common目录下的hadoop-common-2.7.1.jar和haoop-nfs-2.7.1.jar；
- 2、/usr/local/hadoop/share/hadoop/common/lib”目录下的所有JAR包；
- 3、/usr/local/hadoop/share/hadoop/hdfs目录下的haoop-hdfs-2.7.1.jar和haoop-hdfs-nfs-2.7.1.jar；
- 4、/usr/local/hadoop/share/hadoop/hdfs/lib目录下的所有JAR包。
- 5、/usr/local/hadoop/share/hadoop/mapreduce目录下的所有JAR包。
- 6、/usr/local/hadoop/share/hadoop/mapreduce/lib目录下的所有JAR包。
- 7、/usr/local/hadoop/share/hadoop/yarn下的包以及yarn 下的lib目录下的包导入

此时会默认去/user/root/input目录下找文件。

运行报错：

```java
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/yarn/util/Apps。
```

**解决：没有把yarn下的包以及yarn 下的lib目录下的包导入**

## 8、运行结果

![](D:\yunxinjieguo.png)









