[grid@hadoop1 ~]$ cat .bash_profile HADOOP_PREFIX=/home/grid/hadoop-1.2.1 PATH=$PATH:$HOME/bin:$HADOOP_PREFIX/bin export HADOOP_PREFIX PATH
2.準備天氣數據
java
import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class MinTemperatureMapper 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(0, 4); int airTemperature = Integer.parseInt(line.substring(13, 19).trim()); if (airTemperature != MISSING) { context.write(new Text(year), new IntWritable(airTemperature)); } } } import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class MinTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int minValue = Integer.MAX_VALUE; for (IntWritable value : values) { minValue = Math.min(minValue, value.get()); } context.write(key, new IntWritable(minValue)); } } import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.Job; public class MinTemperature { public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: MinTemperature <input path> <output path>"); System.exit(-1); } Job job = new Job(); job.setJarByClass(MinTemperature.class); job.setJobName("Min temperature"); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setMapperClass(MinTemperatureMapper.class); job.setCombinerClass(MinTemperatureReducer.class); job.setReducerClass(MinTemperatureReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
6.編譯並打成JAR包,刪除編譯後的class文件 shell