Hadoop Eclipse下运行WordCount

ubuntu  11.10

用户名:lord

注意了,只有3.3的支持hadoop0.20.2插件

1.hadoop的eclipse插件

复制 hadoop 安装目录下   /contrib/eclipse-plugin/hadoop-0.20.2-eclipse-plugin.jar 到   eclipse安装目录/plugins/ 下。


2.设置Eclipse下Hadoop Map/Reduce

windows--->preference

如图:

Hadoop Eclipse下运行WordCount_第1张图片

3.开启mapReduce视图

window---》show view

Hadoop Eclipse下运行WordCount_第2张图片

4. 配置地址及端口

右键-->New Hadoop Location。在弹出的对话框中你需要配置Location name

Hadoop Eclipse下运行WordCount_第3张图片

5.设置args

点击WordCount.java,右键-->Run As-->Run Configurations

input是你工程下的文件夹,file01是我们的文件

output会在你运行程序时生成,在工程文件夹下

运行前记得bin\start-all.sh!!!

Hadoop Eclipse下运行WordCount_第4张图片


附加WordCount代码:

package edu.btbu.cs714;



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{
    
    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 {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable 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  ");
      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);
  }
}

运行即可


你可能感兴趣的:(eclipse,hadoop,eclipse插件,mapreduce,exception,class,linux,hadoop)