气象大数据案例项目(求各气象站的均匀气温)

打印 上一主题 下一主题

主题 843|帖子 843|积分 2531

一、项目需求

现在有一份来自美国国家海洋和大气管理局的数据集,里面包罗近30年每个气象站、每小时的气候预报数据,每个报告的文件巨细大约15M。一共有10个气象站,每个报告文件的名字包罗气象站ID,每条记录包罗气温、风向、气候状况等多个字段信息。现在要求统计美国各气象站30年均匀气温。
二、数据格式


一共10份气象站的数据

文档里面的数据格式,注意 -9999 说明数据缺失

三、项目开辟

3.1 在windows 进行开辟



  • 引入 Hadoop 依靠
  1. <dependency>
  2.         <groupId>org.apache.hadoop</groupId>
  3.         <artifactId>hadoop-client</artifactId>
  4.         <version>2.10.2</version>
  5. </dependency>
复制代码


  • 开辟脚本
  1. package com.feifei.mapreduce;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.IntWritable;
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapred.FileSplit;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.Reducer;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  14. import java.io.IOException;
  15. public class WeatherAnalysis {
  16.     public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {
  17.         @Override
  18.         protected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  19.             String line = value.toString();
  20.             int temperature = Integer.parseInt(line.substring(14, 19).trim());
  21.             if(temperature != -9999){
  22.                 FileSplit fileSplit = (FileSplit) context.getInputSplit();
  23.                 String id = fileSplit.getPath().getName().substring(5, 10);
  24.                 context.write(new Text(id), new IntWritable(temperature));
  25.             }
  26.         }
  27.     }
  28.     public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
  29.         private IntWritable mean = new IntWritable();
  30.         @Override
  31.         protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
  32.             int sum = 0;
  33.             int count = 0;
  34.             for (IntWritable val : values) {
  35.                 sum += val.get();
  36.             }
  37.             mean.set(sum / count);
  38.             context.write(key, mean);
  39.         }
  40.     }
  41.     public static void main(String[] args) throws Exception {
  42.         Configuration conf = new Configuration();
  43.         Job job = Job.getInstance(conf);
  44.         job.setJarByClass(WeatherAnalysis.class);
  45.         job.setJobName("WeatherAnalysis");
  46.         job.setInputFormatClass(TextInputFormat.class);
  47.         job.setOutputFormatClass(TextOutputFormat.class);
  48.         FileInputFormat.addInputPath(job, new Path(args[0]));
  49.         FileOutputFormat.setOutputPath(job, new Path(args[1]));
  50.         job.setMapperClass(WeatherAnalysis.MyMapper.class);
  51.         job.setReducerClass(WeatherAnalysis.MyReducer.class);
  52.         job.setMapOutputKeyClass(Text.class);
  53.         job.setMapOutputValueClass(IntWritable.class);
  54.         job.setOutputKeyClass(Text.class);
  55.         job.setOutputValueClass(IntWritable.class);
  56.         job.waitForCompletion(true);
  57.     }
  58. }
复制代码



  • 设置入参和保存路径

3.2 运行效果


3.3 对项目打包

  1. mvn clean package
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

拉不拉稀肚拉稀

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表