MapReduce求海量数据中的最大值

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


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

[java]  view plain copy
  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. */  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值