我相信所有hadoop爱好者自己动手写的第一个程序都是求最高气温,下面大家可以参考一下
Hadoop中求最高气温 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; class Map extends Mapper<LongWritable,Text,Text,IntWritable>{ @Override public void map (LongWritable key, Text value, 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)); } } class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{ @Override 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(new Text(key), new IntWritable(maxValue)); } } public class TemperatureMax { public static void main(String[] args) throws Exception{ if (args.length != 2){ System.err.println("can shu shao yu liang ge"); System.exit(-1); } Job job = new Job(); job.setJarByClass( TemperatureMax.class); job.setJobName("MyFirst"); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); System.exit(job.waitForCompletion(true)?0:1); } }
Hadoop中求最高气温
最新推荐文章于 2024-04-28 20:02:00 发布