2018年安徽省大数据比赛MapReduce题目解答第三题

男娘i 2023-08-17 17:48 199阅读 0赞

3、求每年最高气温(10分)

根据/opt/ahdsjjs/Temperature.txt编写MapperReduce脚本 获得每年的最高气温,并输出

数据格式如下:

  1. 2014010114
  2. 2014010216
  3. 2014010317
  4. 2014010410
  5. 2014010506
  6. 2012010609
  7. 2012010732
  8. 2012010812
  9. 2012010919
  10. 2012011023
  11. 2001010116
  12. 2001010212
  13. 2001010310
  14. 2001010411
  15. 2001010529
  16. 2013010619
  17. 2013010722
  18. 2013010812
  19. 2013010929
  20. 2013011023
  21. 2008010105
  22. 2008010216
  23. 2008010337
  24. 2008010414
  25. 2008010516
  26. 2007010619
  27. 2007010712
  28. 2007010812
  29. 2007010999
  30. 2007011023
  31. 2010010114
  32. 2010010216
  33. 2010010317
  34. 2010010410
  35. 2010010506
  36. 2015010649
  37. 2015010722
  38. 2015010812
  39. 2015010999
  40. 2015011023

比如:2010012325表示在2010年01月23日的气温为25度。

现在要求使用MapReduce,计算每一年出现过的最大气温

思路:其实就是字符串的截取,那到年,以年为key进行分组,天气为value

主函数:

  1. import java.io.IOException;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.TreeMap;
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.LongWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Reducer;
  9. public class Myred extends Reducer<Text, IntWritable, Text, IntWritable> {
  10. @Override
  11. protected void reduce(Text key, Iterable<IntWritable> values,
  12. Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  13. /**
  14. * 2014 14
  15. 2014 16
  16. 2014 17
  17. 2014 10
  18. 2014 06
  19. */
  20. int maxval = Integer.MIN_VALUE;
  21. int max2val = -10000000;
  22. for (IntWritable val : values) {
  23. maxval = Math.max(maxval, val.get());
  24. }
  25. context.write(new Text(key+" "+maxval), new IntWritable(max2val));
  26. }
  27. }

Map函数:

  1. import java.io.IOException;
  2. import org.apache.hadoop.io.IntWritable;
  3. import org.apache.hadoop.io.LongWritable;
  4. import org.apache.hadoop.io.Text;
  5. import org.apache.hadoop.mapreduce.Mapper;
  6. import tq.TQ;
  7. public class Mymap extends Mapper<LongWritable, Text, Text, IntWritable>{
  8. @Override
  9. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
  10. throws IOException, InterruptedException {
  11. // 2014010506
  12. // 2012010609
  13. String line = value.toString();
  14. String yera = line.substring(0,6);
  15. int Tem = Integer.parseInt(line.substring(8,10));
  16. context.write(new Text(yera), new IntWritable(Tem));
  17. }
  18. }

Reduce函数:

  1. import java.io.IOException;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.TreeMap;
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.LongWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Reducer;
  9. public class Myred extends Reducer<Text, IntWritable, Text, IntWritable> {
  10. @Override
  11. protected void reduce(Text key, Iterable<IntWritable> values,
  12. Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  13. /**
  14. * 2014 14
  15. 2014 16
  16. 2014 17
  17. 2014 10
  18. 2014 06
  19. */
  20. int maxval = Integer.MIN_VALUE;
  21. int max2val = -10000000;
  22. for (IntWritable val : values) {
  23. maxval = Math.max(maxval, val.get());
  24. }
  25. context.write(new Text(key+" "+maxval), new IntWritable(max2val));
  26. }
  27. }

发表评论

表情:
评论列表 (有 0 条评论,199人围观)

还没有评论,来说两句吧...

相关阅读

    相关 2018数据面试总结

    目前面试了多家大数据开发工程师,成长了很多,也知道了很多知识,下面和大家分享一下我遇到的面试题和答案。 1.kafka集群的规模,消费速度是多少。 答:一般中小型公司是10