Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631194
  • 博文数量: 87
  • 博客积分: 3399
  • 博客等级: 中校
  • 技术积分: 1422
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 21:20
文章分类

全部博文(87)

文章存档

2013年(1)

2012年(51)

2011年(33)

2010年(2)

分类: 云计算

2012-12-26 23:13:22

1 创建Map/Reduce 工程

File->new->project->Map/Reduce Project

2 创建类WordCount

File->new->class

3 设置输入输出参数

Run->Run Configuration, Arguments

program arguments中写入输入输出参数,即输入输出文件路径:

hdfs://localhost:9000/user/leo/  hdfs://localhost:9000/user/out

4 run as java application


点击(此处)折叠或打开

  1. import java.io.IOException;
  2. import java.util.StringTokenizer;

  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.Reducer;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. import org.apache.hadoop.util.GenericOptionsParser;

  13. public class WordCount {

  14.   public static class TokenizerMapper
  15.        extends Mapper<Object, Text, Text, IntWritable>{

  16.     private final static IntWritable one = new IntWritable(1);
  17.     private Text word = new Text();

  18.     public void map(Object key, Text value, Context context
  19.                     ) throws IOException, InterruptedException {
  20.       StringTokenizer itr = new StringTokenizer(value.toString());
  21.       while (itr.hasMoreTokens()) {
  22.         word.set(itr.nextToken());
  23.         context.write(word, one);
  24.       }
  25.     }
  26.   }

  27.   public static class IntSumReducer
  28.        extends Reducer<Text,IntWritable,Text,IntWritable> {
  29.     private IntWritable result = new IntWritable();

  30.     public void reduce(Text key, Iterable<IntWritable> values,
  31.                        Context context
  32.                        ) throws IOException, InterruptedException {
  33.       int sum = 0;
  34.       for (IntWritable val : values) {
  35.         sum += val.get();
  36.       }
  37.       result.set(sum);
  38.       context.write(key, result);
  39.     }
  40.   }

  41.   public static void main(String[] args) throws Exception {
  42.     Configuration conf = new Configuration();
  43.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  44.     if (otherArgs.length != 2) {
  45.       System.err.println("Usage: wordcount ");
  46.       System.exit(2);
  47.     }
  48.     Job job = new Job(conf, "word count");
  49.     job.setJarByClass(WordCount.class);
  50.     job.setMapperClass(TokenizerMapper.class);
  51.     job.setCombinerClass(IntSumReducer.class);
  52.     job.setReducerClass(IntSumReducer.class);
  53.     job.setOutputKeyClass(Text.class);
  54.     job.setOutputValueClass(IntWritable.class);
  55.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  56.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  57.     System.exit(job.waitForCompletion(true) ? 0 : 1);
  58.   }
  59. }

阅读(6031) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~