当前位置: 首页 > news >正文

hadoop案例实践:气象大数据离线分析

引入Hadoop依赖

<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-client</artifactId>
  <version>2.9.2</version>
</dependency>

MyMapper.java

package com.hadoop;

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 org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException, IOException {
            String line = value.toString();
            int temperature = Integer.parseInt(line.substring(14, 19).trim());
            if (temperature != -9999) {
                FileSplit failsplit = (FileSplit) context.getInputSplit();
                String id = failsplit.getPath().getName().substring(5, 10);
                //输出气象站id
                context.write(new Text(id), new IntWritable(temperature));
            }

        }
}

MyReducer.java

package com.hadoop;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable sean = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context)
            throws IOException, InterruptedException, IOException {
        int sum = 0;
        int count = 0;
        for (IntWritable val : values) {
            sum += val.get();
            count++;
        }
        //求平均值气温
        sean.set(sum / count);
        context.write(key, sean);
    }
}

MyDriver.java

package com.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import java.io.IOException;
public class MyDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "WeatherAnalysis");
        job.setJarByClass(MyDriver.class);
        //输入输出路径
        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //输入输出格式
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        //设置mapper及map输出的key value类型
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //设置Reducer及reduce输出key value类型
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.waitForCompletion(true);
    }
}

 jar打包

终端输入:

mvn clean package

进入虚拟机

1.开启集群:

2.

hadoop fs -mkdir /weather

3.上传数据源

4.hdfs dfs -put 上传的目录地址/* /weather

 4运行任务

yarn jar jar包地址 com.xxx.主类 /weather /out

/out:不存在的文件夹。

5.查看结果

hadoop fs -cat /out/part*

相关文章:

  • 悬挂指针与野指针:如何避免常见内存问题
  • MySql学习_基础Sql语句
  • WSL 中的高级设置配置
  • kkFileView文件预览组件部署说明
  • 工控hmi医疗终端机的界面如何来设计?本文为你解答
  • Spring Boot 参数校验异常与错误编码映射方案
  • 什么是净利润
  • BGP实验(二)—路由反射器
  • 对vue VS react的理解
  • 【网络安全】CSV注入(附实战案例)
  • C#—闭包详解
  • 【docker】端口暴露
  • Webservice如何调用
  • zabbix学习笔记
  • 前端开发:Web蜜罐详解
  • Java实例化详解:从概念到实践的全方位解读
  • 如何把绿色可执行应用程序添加到Ubuntu的收藏夹Dock中
  • 【DvAdmin】基于腾讯云Cos实现资源预签名访问
  • 硬件工程师入门教程(四)
  • k8s面试题总结(十五)
  • 福特中国CFO:依然坚信中国市场,上海帮助公司吸引到人才
  • 中国戏剧奖梅花奖终评启动在即,17场演出公益票将发售
  • 安且吉兮,西泠印社雅集吴昌硕故里
  • 尹锡悦涉嫌发动内乱案第二次庭审举行
  • 同济研究生开发AI二维码拿下大奖,新一代00后开发者掀起AI创业潮
  • 从板凳席到指挥台,横扫广东男篮的少帅潘江究竟有何神奇