基于hadoop 1.0.4版本的气象温度统计源代码

 

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;
import org.apache.hadoop.util.GenericOptionsParser;

/**
 * 统计气象温度,基于hadoop1.0.4版本
 * @author fengbo
 *
 */
public class StatisticTemp
{
 public static class AirTemperatureMapper extends
   Mapper<Object, Text, Text, IntWritable>
 {
  public void map(Object key, Text value,
    Mapper<Object, Text, Text, IntWritable>.Context context)
    throws IOException, InterruptedException
  {
   String line = value.toString();//获取到一行记录
    String year = line.substring(15, 19);//获取到每一年
          int airTemperature;
          if (line.charAt(25) == '+') {
              airTemperature = Integer.parseInt(line.substring(26, 30));
          } else {
              airTemperature = Integer.parseInt(line.substring(25, 30));
          }
          context.write(new Text(year), new IntWritable(airTemperature));
  }
 }

 public static class AirTemperatureMapperReducer extends
   Reducer<Text, IntWritable, Text, IntWritable>
 {
  public void reduce(Text key, Iterable<IntWritable> values,
    Reducer<Text, IntWritable, Text, IntWritable>.Context context)
    throws IOException, InterruptedException
  {
   int maxValue = Integer.MIN_VALUE;
        for(IntWritable val : values) {
             maxValue = Math.max(maxValue, val.get());
         }
         context.write(key, new IntWritable(maxValue));
  }
 }

 public static void main(String[] args) throws Exception
 {
  Configuration conf = new Configuration();
  String[] otherArgs = new GenericOptionsParser(conf, args)
    .getRemainingArgs();
  if (otherArgs.length != 2)
  {
   System.err.println("Usage: AirTemperatureMapper <in> <out>");
   System.exit(2);
  }
  Job job = new Job(conf, "AirTemperatureMapper");
  job.setJarByClass(StatisticTemp.class);
  job.setMapperClass(AirTemperatureMapper.class);
  job.setCombinerClass(AirTemperatureMapperReducer.class);
  job.setReducerClass(AirTemperatureMapperReducer.class);
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(IntWritable.class);
  FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  System.exit(job.waitForCompletion(true) ? 0 : 1);
 }

}

 

你可能感兴趣的:(hadoop,气象温度统计源代码)