(1) 源代码
package cn.edu.xjtu.temperature; import java.io.IOException; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class MaxTemperature { static class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private static final int MISSING = 9999; public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{ String line = value.toString(); String year = line.substring(15, 19); System.out.println(year); int airTemperature; if(line.charAt(40) == '+'){ airTemperature = Integer.parseInt(line.substring(41,45)); }else{ airTemperature = Integer.parseInt(line.substring(40,45)); } String quality = line.substring(45,46); if(airTemperature != MISSING && quality.matches("[01459]")){ context.write(new Text(year), new IntWritable(airTemperature)); } } } static class MaxTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int maxValue = Integer.MIN_VALUE; for(IntWritable value : values){ maxValue = Math.max(maxValue, value.get()); } context.write(key, new IntWritable(maxValue)); } } public static void main(String[] args) throws Exception { if(args.length != 2){ System.err.println("Usage: MaxTemperature <input path> <output path>"); System.exit(-1); } Job job = new Job(); job.setJarByClass(MaxTemperature.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(MaxTemperatureMapper.class); job.setCombinerClass(MaxTemperatureReducer.class); job.setReducerClass(MaxTemperatureReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true)? 0 : 1); } }
0067011990999991950051507004...9999999N9+00001+99999999999... 0043011990999991950051512004...9999999N9+00221+99999999999... 0043011990999991950051518004...9999999N9-00111+99999999999... 0043012650999991949032412004...0500001N9+01111+99999999999... 0043012650999991949032418004...0500001N9+00781+99999999999...
4. 执行
(1) [hadoop@node14 app]$ hadoop fs -put record.txt input.txt
(2) [hadoop@node14 app]$ hadoop jar maxTemperature.jar cn.edu.xjtu.temperature.MaxTemperature input.txt outputDir
(3) 查看执行结果
[hadoop@node14 app]$ hadoop fs -ls outputDir
[hadoop@node14 app]$ hadoop fs -cat outputDir/part-r-00000
1949 111
1950 22