Created
November 3, 2014 04:42
-
-
Save likejazz/059d5e024ebb8430c4dd to your computer and use it in GitHub Desktop.
O'Reilly's "Hadoop: The Definitive Guide" ch2 sample in a one file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cc MaxTemperature Application to find the maximum temperature in the weather dataset | |
// vv MaxTemperature | |
import java.io.IOException; | |
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.lib.input.FileInputFormat; | |
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; | |
import org.apache.hadoop.io.LongWritable; | |
import org.apache.hadoop.mapreduce.Mapper; | |
import org.apache.hadoop.mapreduce.Reducer; | |
public class MaxTemp { | |
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(MaxTemp.class); | |
job.setJobName("Max temperature"); | |
FileInputFormat.addInputPath(job, new Path(args[0])); | |
FileOutputFormat.setOutputPath(job, new Path(args[1])); | |
job.setMapperClass(MaxTemperatureMapper.class); | |
job.setReducerClass(MaxTemperatureReducer.class); | |
job.setOutputKeyClass(Text.class); | |
job.setOutputValueClass(IntWritable.class); | |
System.exit(job.waitForCompletion(true) ? 0 : 1); | |
} | |
public static class MaxTemperatureMapper | |
extends Mapper<LongWritable, Text, Text, IntWritable> { | |
private static final int MISSING = 9999; | |
@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(87) == '+') { // parseInt doesn't like leading plus signs | |
airTemperature = Integer.parseInt(line.substring(88, 92)); | |
} else { | |
airTemperature = Integer.parseInt(line.substring(87, 92)); | |
} | |
String quality = line.substring(92, 93); | |
if (airTemperature != MISSING && quality.matches("[01459]")) { | |
context.write(new Text(year), new IntWritable(airTemperature)); | |
} | |
} | |
} | |
public static class MaxTemperatureReducer | |
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(key, new IntWritable(maxValue)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment