MapReduce计算气温原始数据

本文介绍了一个使用Hadoop进行天气数据处理的MapReduce程序,该程序能够从原始天气数据中提取年度最高、最低及平均气温。通过对数据的截取和筛选,确保了采集到的温度数据的准确性和有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

  • 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));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百世修行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值