Linux下使用Eclipse编写MapReduce程序的配置


最近一直都在看《Hadoop权威指南(中文版)》,虽然的确是在翻译的方面有很多不如意之处,但是对于我这个英语不是很好的人来说,看中文版的书还是能够大大节约我的时间的。我的本科毕业设计就是关于HDFS和MapReduce的,所以我最近非常想马上编写出自己的MapReduce程序。

从网上看到了一个关于题目所说的非常好的配置方法,自己动手试了试,发现果然非常好用,所以将这个文章转载如下:

1、确定eclipse是关闭的,如果不是的话,弄好之后要关了重新打开才可以。找到hadoop的安装路径,我的是hadoop-0.20.2,在home/hadoop/hadoop-0.20.2/contrib/eclipse-plugin/下有hadoop-0.20.2-eclipse-plugin.jar,将这个jar包拷贝到eclipse安装目录下的plugins里,我的是在usr/eclipse/plugins/下,然后打开eclipse,点击主菜单上的window—preferences,在左边栏中找到Hadoop Map/Reduce,点击后在右边对话框里设置hadoop的安装路径即主目录。

2、创建一个MapReduce Project,点击eclipse主菜单上的File—New—Project,在弹出的对话框中选择MapReduce Project,之后输入Project的名,例如wordcount,确定即可,然后就可以象一个普通的 Eclipse Java project 那样,添加Java类,比如你可以定义一个WordCount 类,然后将你安装的hadoop程序里的WordCount源程序代码(版本不同会有区别),我的是在hadoop-0.20.2/src/examples/org/apache/hadoop/examples/WordCount.java写到此类中(以下是源程序代码),如果是19版本以前的,添加入必要的 import 语句 ( Eclipse 快捷键 ctrl+shift+o 可以帮你),即可形成一个完整的 wordcount 程序,然后运行。

import java.io.IOException;
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 static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{
   
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
     
    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }
 
  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length != 2) {
      System.err.println("Usage: wordcount <in> <out>");
      System.exit(2);
    }
    Job job = new Job(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

  • 1
  • 2
  • 下一页

相关内容