你好,游客 登录 注册 搜索
背景:
阅读新闻

MapReduce求海量数据中的最大值

[日期:2012-05-13] 来源:Linux社区  作者:jtlyuan [字体:大 中 小]

利用MapReduce求解海量数据文件中的最大值

思路:利用Mapper类中的cleanup()函数,因为cleanup()函数是在所有的map()完成之后才执行的。

  1. package jtlyuan.csdn;  
  2. import java.io.IOException;  
  3. import org.apache.Hadoop.conf.Configuration;  
  4. import org.apache.hadoop.conf.Configured;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.LongWritable;  
  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.input.TextInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  16. import org.apache.hadoop.util.Tool;  
  17. import org.apache.hadoop.util.ToolRunner;  
  18. //求最大值   
  19. public class MaxNum extends Configured implements Tool {  
  20. public static class MapClass extends Mapper<LongWritable, Text, IntWritable, IntWritable> {  
  21. private int maxNum = 0;  
  22. public void map(LongWritable key, Text value, Context context)  
  23. throws IOException, InterruptedException {  
  24. String[] str = value.toString().split(",", 2);  
  25. try {// 对于非数字字符我们忽略掉   
  26. int temp = Integer.parseInt(str[0]);  
  27. if (temp > maxNum) {  
  28. maxNum = temp;  
  29. }  
  30. } catch (NumberFormatException e) {  
  31. }  
  32. }  
  33. @Override  
  34. protected void cleanup(Context context) throws IOException,  
  35. InterruptedException {  
  36. context.write(new IntWritable(maxNum), new IntWritable(maxNum));  
  37. }  
  38. }  
  39. public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {  
  40. private int maxNum = 0;  
  41. public void reduce(IntWritable key, Iterable<IntWritable> values, Context context)  
  42. throws IOException, InterruptedException {  
  43. for (IntWritable val : values) {  
  44. if ( val.get() > maxNum) {  
  45. maxNum = val.get();  
  46. }  
  47. }  
  48. }  
  49. @Override  
  50. protected void cleanup(Context context) throws IOException,  
  51. InterruptedException {  
  52. context.write(new IntWritable(maxNum), new IntWritable(maxNum));  
  53. }  
  54. }  
  55. public int run(String[] args) throws Exception {  
  56. Configuration conf = getConf();  
  57. Job job = new Job(conf, "MaxNum");  
  58. job.setJarByClass(MaxNum.class);  
  59. FileInputFormat.setInputPaths(job, new Path(args[0]));  
  60. FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  61. job.setMapperClass(MapClass.class);  
  62. job.setCombinerClass(Reduce.class);  
  63. job.setReducerClass(Reduce.class);  
  64. job.setInputFormatClass(TextInputFormat.class);  
  65. job.setOutputFormatClass(TextOutputFormat.class);  
  66. job.setOutputKeyClass(IntWritable.class);  
  67. job.setOutputValueClass(IntWritable.class);  
  68. System.exit(job.waitForCompletion(true) ? 0 : 1);  
  69. return 0;  
  70. }  
  71. public static void main(String[] args) throws Exception {  
  72. long start = System.nanoTime();  
  73. int res = ToolRunner.run(new Configuration(), new MaxNum(), args);  
  74. System.out.println(System.nanoTime()-start);  
  75. System.exit(res);  
  76. }  
  77. }  
  78. /* 
  79. * 最终输出:6009554 6009554 
  80. * 
  81. */  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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