1 package com.month10b.weather;
 2 
 3 import org.apache.hadoop.conf.Configuration;
 4 import org.apache.hadoop.fs.Path;
 5 import org.apache.hadoop.io.IntWritable;
 6 import org.apache.hadoop.io.LongWritable;
 7 import org.apache.hadoop.io.Text;
 8 import org.apache.hadoop.mapreduce.Job;
 9 import org.apache.hadoop.mapreduce.Mapper;
10 import org.apache.hadoop.mapreduce.Reducer;
11 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
12 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
13 
14 import java.io.IOException;
15 
16 public class TemperatureWeather {
17 
18     static class TempMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
19         @Override
20         public void map(LongWritable key, Text value, Context context)
21                 throws IOException, InterruptedException {
22             // 打印样本: Before Mapper: 0, 2000010115
23             System.out.print("Before Mapper: " + key + ", " + value);
24             String line = value.toString();
25             String year = line.substring(0, 4);
26             int temperature = Integer.parseInt(line.substring(8));
27             context.write(new Text(year), new IntWritable(temperature));
28             // 打印样本: After Mapper:2000, 15
29             System.out.println("======" + "After Mapper:" + new Text(year) + ", " + new IntWritable(temperature));
30         }
31     }
32 
33     static class TempReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
34         @Override
35         public void reduce(Text key, Iterable<IntWritable> values,
36                            Context context) throws IOException, InterruptedException {
37             int maxValue = Integer.MIN_VALUE;
38             StringBuffer sb = new StringBuffer();
39             //取values的最大值
40             for (IntWritable value : values) {
41                 maxValue = Math.max(maxValue, value.get());
42                 sb.append(value).append(", ");
43             }
44             // 打印样本: Before Reduce: 2000, 15, 23, 99, 12, 22,
45             System.out.print("Before Reduce: " + key + ", " + sb.toString());
46             context.write(key, new IntWritable(maxValue));
47             // 打印样本: After Reduce: 2000, 99
48             System.out.println( "======" +   "After Reduce: " + key + ", " + maxValue);
49         }
50     }
51 
52 
53 
54 
55 
56     public static void main(String[] args) throws Exception {
57         //输入路径
58         String dst = "hdfs://localhost:9000/intput.txt";
59         //输出路径,必须是不存在的,空文件加也不行。
60         String dstOut = "hdfs://localhost:9000/output";
61         Configuration hadoopConfig = new Configuration();
62 
63         hadoopConfig.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName() );
64         hadoopConfig.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName()  );
65         Job job = new Job(hadoopConfig);
66 
67         //如果需要打成jar运行,需要下面这句
68         //job.setJarByClass(NewMaxTemperature.class);
69 
70         //job执行作业时输入和输出文件的路径
71         FileInputFormat.addInputPath(job, new Path("C:\\Users\\Dell\\Desktop\\week03\\qiwen_input\\qiwen.txt"));
72         FileOutputFormat.setOutputPath(job, new Path("C:\\Users\\Dell\\Desktop\\week03\\qiwen_output"));
73 
74         //指定自定义的Mapper和Reducer作为两个阶段的任务处理类
75         job.setMapperClass(TempMapper.class);
76         job.setReducerClass(TempReducer.class);
77 
78         //设置最后输出结果的Key和Value的类型
79         job.setOutputKeyClass(Text.class);
80         job.setOutputValueClass(IntWritable.class);
81 
82         //执行job,直到完成
83         job.waitForCompletion(true);
84         System.out.println("Finished");
85     }
86 
87 
88 }