手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop2.0 Mapreduce实例WordCount体验

[日期:2012-10-22] 来源:Linux社区  作者:xubcing [字体:大 中 小]

在Mapper和Reducer类中,都有一个run()方法不断提供(key,value)来调用map()和reduce()函数来处理,我们一般只需重写其中的map和reduce方法。在mapreduce中只有支持序列化的类才能作为键值,其中的key还必须要是可比较的,故 key要实现WritableComparable接口,value只需要实现Writable接口。

如下给出自己参照源码写的MyWordCount.java

  1. import java.io.IOException; 
  2. import java.util.Iterator; 
  3. import java.util.StringTokenizer; 
  4.  
  5. import org.apache.Hadoop.conf.Configuration; 
  6. import org.apache.hadoop.fs.Path; 
  7. import org.apache.hadoop.io.IntWritable; 
  8. import org.apache.hadoop.io.Text; 
  9. import org.apache.hadoop.mapreduce.Job; 
  10. import org.apache.hadoop.mapreduce.Mapper; 
  11. import org.apache.hadoop.mapreduce.Reducer; 
  12. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
  14. import org.apache.hadoop.util.GenericOptionsParser; 
  15.  
  16. public class MyWordCount { 
  17.  
  18.     public static class WordCountMapper extends Mapper<Object,Text,Text,IntWritable> { 
  19.          
  20.         private static final IntWritable one =  new IntWritable(1); 
  21.          
  22.         private Text word = new Text(); 
  23.  
  24.         protected void map(Object key, Text value, Context context) 
  25.                 throws IOException, InterruptedException { 
  26.             String line = value.toString(); 
  27.             StringTokenizer words = new StringTokenizer(line); 
  28.             while(words.hasMoreTokens()) { 
  29.                 word.set(words.nextToken()); 
  30.                 context.write(word, one); 
  31.             } 
  32.         } 
  33.     } 
  34.      
  35.     public static class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable> { 
  36.  
  37.         private IntWritable totalNum = new IntWritable(); 
  38.          
  39.         @Override 
  40.         protected void reduce(Text key, Iterable<IntWritable> values,Context context) 
  41.                 throws IOException, InterruptedException { 
  42.            int sum = 0; 
  43.            Iterator<IntWritable> it = values.iterator(); 
  44.                    while(it.hasNext()) { 
  45.                    sum += it.next().get(); 
  46.                    } 
  47.                    totalNum.set(sum); 
  48.                    context.write(key,totalNum);   
  49.         } 
  50.     } 
  51.      
  52.     public static void main(String[] args) throws Exception{ 
  53.         Configuration conf = new Configuration();             
  54.  
  55.         Job job = new Job(conf,"MyWordCount"); 
  56.          
  57.         job.setJarByClass(MyWordCount.class); //设置运行jar中的class名称  
  58.          
  59.         job.setMapperClass(WordCountMapper.class);//设置mapreduce中的mapper reducer combiner类  
  60.         job.setReducerClass(WordCountReducer.class); 
  61.                 job.setCombinerClass(WordCountReducer.class);   
  62.          
  63.         job.setOutputKeyClass(Text.class); //设置输出结果键值对类型  
  64.                 job.setOutputValueClass(IntWritable.class); 
  65.          
  66.         FileInputFormat.addInputPath(job,new Path(args[0]));//设置mapreduce输入输出文件路径  
  67.         FileOutputFormat.setOutputPath(job,new Path(args[1])); 
  68.          
  69.         System.exit(job.waitForCompletion(true) ? 0:1); 
  70.     } 
  71.      
  72. } 
linux
相关资讯       Hadoop  MapReduce  Mapreduce实例  WordCount  WordCount源码 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款