MapReduce——计算温度最大值 (基于全新2.2.0API)

MapReduce——计算温度最大值 (基于全新2.2.0API)


 

deprecated: Job类的所有Constructors, 新的API用静态方法getInstance(conf)来去的Job的实例;

Code:

 1 import java.io.IOException;

 2 import java.util.Iterator;

 3 

 4 import org.apache.hadoop.fs.FileSystem;

 5 import org.apache.hadoop.fs.Path;

 6 import org.apache.hadoop.conf.Configuration;

 7 import org.apache.hadoop.conf.Configured;

 8 import org.apache.hadoop.io.LongWritable;

 9 import org.apache.hadoop.io.Text;

10 import org.apache.hadoop.io.IntWritable;

11 import org.apache.hadoop.mapreduce.Mapper;

12 import org.apache.hadoop.mapreduce.Reducer;

13 import org.apache.hadoop.util.Tool;

14 import org.apache.hadoop.util.ToolRunner;

15 import org.apache.hadoop.mapreduce.Job;

16 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

17 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

18 

19 public class getMaxTemperature  extends Configured implements Tool {

20 

21   class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

22     @Override

23     public void map(LongWritable key, Text val, Context context) throws IOException, InterruptedException {

24       String line = val.toString();

25       String year = line.substring(15, 19);

26     

27       int Temperature;

28       if (!hasPlus(line)){

29         Temperature = Integer.parseInt(line.substring(87, 92));

30       } else {

31         Temperature = Integer.parseInt(line.substring(88,92));

32       }   

33       String qual = line.substring(92, 93);

34       if(!matched(qual)) {

35         context.write(new Text(year), new IntWritable(Temperature));

36    }

37 

38     }

39 

40     private boolean hasPlus(String line) {

41       return line.charAt(87) == '+' ?  true : false;

42     }

43 

44     private boolean matched(String line) {

45         return line.matches("[01459") ? true : false;

46     }

47 

48   }

49 

50   class MaxTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

51     @Override

52     public void reduce(Text key, Iterable<IntWritable> vals, Context context) throws IOException, InterruptedException {

53       int maxValue = Integer.MIN_VALUE;

54       for( IntWritable value : vals ) {

55         maxValue = Math.max(maxValue, value.get());

56       }

57       context.write(key, new IntWritable(maxValue));

58     }

59   }

60 

61     @Override

62     public int run(String[] args) throws Exception {

63       Configuration conf = getConf();

64       Job job = Job.getInstance(conf);

65       job.setJobName("helloRuby");

66       job.setJarByClass(getClass());

67       FileInputFormat.addInputPath(job, new Path(args[0]));

68       FileOutputFormat.setOutputPath(job, new Path(args[1]));

69 

70       job.setMapperClass(MaxTemperatureMapper.class);

71       job.setCombinerClass(MaxTemperatureReducer.class);

72       job.setReducerClass(MaxTemperatureReducer.class);

73 

74       job.setOutputKeyClass(Text.class);

75       job.setOutputValueClass(IntWritable.class);

76 

77       return job.waitForCompletion(true) ? 0 : 1;

78     }

79 

80 

81   public static void main(String[] args) throws Exception {

82     ToolRunner.run(new getMaxTemperature() , args);

83   }

84 }

85 

                                                                        

你可能感兴趣的:(mapreduce)