/**
- 1.根据天气的原始数据,通过截取的方式,找出需要的天气
- 2.截取指定字符串的天气
*/
数据下载
package com.bipt.model.wether;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* 1.根据天气的原始数据,通过截取的方式,找出需要的天气
* 2.截取指定字符串的天气
*/
public class MrTemperature extends Configured implements Tool {
public static class ModelMap extends Mapper<LongWritable, Text, Text, IntWritable> {
int MESSING = 9999;// 没有采集到天气的状态值
Text k = new Text();
IntWritable v = new IntWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String year = line.substring(15, 19);
int airTemperature = 0;
if (line.charAt(87) == '+') {// 数据结构中+、-代表的温度的正负
// 数据源中:88-92位置 表示温度
airTemperature = Integer.parseInt(line.substring(88, 92));
} else {
// 零度以下,需要带负号 87是符号位置
airTemperature = Integer.parseInt(line.substring(87, 92));
}
// 空气质量是 数据源中的位置 92-93; 质量需要时 0/1/4/5/9中的一种才属于正常情况
String quality = line.substring(92, 93);
if (airTemperature != MESSING && quality.matches("[01459]")) {
//满足这个固定条件,才属于采集到的正常的温度
k.set(year);
v.set(airTemperature);
context.write(k, v);//存入key年;value温度
}
}
}
public static class ModelReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
Text k = new Text();
IntWritable v = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sumT = 0;// 累加总温度
int index = 0;
int maxT = Integer.MIN_VALUE;//初始最小,用于保存出现的最大值
int minT = Integer.MAX_VALUE;
for (IntWritable v : values) {
int c = v.get();
if (c > maxT) {maxT = c;}
if (c < minT) {minT = c;}
sumT += c;// 计算总温度
index++;
}
String year = key.toString();
k.set(year + "maxTemp:");
v.set(maxT);
context.write(k, v);
k.set(year + "minTemp:");
v.set(minT);
context.write(k, v);
if (index != 0) {
k.set(year + "avg :");
v.set(sumT / index);
context.write(k, v);
}
}
}
@Override
public int run(String[] args) throws Exception {
// 判断
if (args.length < 2) {
System.out.println("error >>> <in> ....<out>");
System.out.println("参数有问题");
System.exit(2);
}
// 初始化信息
Configuration conf = getConf();
Job job = Job.getInstance(conf);
job.setJobName("MrTemp");// 设置工作的名字
job.setJarByClass(MrTemperature.class);
// 设置 mapper有关
job.setMapperClass(ModelMap.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
// 设置reduce有关
job.setReducerClass(ModelReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 启动事件
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
System.exit(ToolRunner.run(new MrTemperature(), args));
}
}