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.开启集群:

复制代码
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*
相关推荐
是Dream呀14 分钟前
Harness 工程:让 Agent 真正把任务做完
人工智能·分布式·缓存·agent
yt004yt38 分钟前
绿岛数字化平台搭建:VOCs 监测与能碳数据一体化方案
大数据·分布式
广州宏帝箱包1 小时前
出口背包的包装有没有防潮、防摔的加固处理
大数据·人工智能
东方护航数据恢复(深圳)2 小时前
本地小算力设备数据恢复经典案例实操全解_东方护航数据恢复深圳店
大数据·服务器·算法·ai·gpu算力
goujunwe2 小时前
企业 GEO 落地三步法:诊断、内容重构、AI 引用效果监测
大数据·人工智能·学习方法·传媒
Geeys2 小时前
拼多多新店推广实操教程
大数据
hrrrrxeeeee2 小时前
文件读取→比对→风险标记,拆解采购 AI 完整工作链路
大数据·人工智能·机器学习·prompt
天天喝旺仔3 小时前
分布式服务容错实战:用 Sentinel 实现限流、熔断与降级
分布式·微服务·云原生·sentinel
科创致远3 小时前
科创致远 ESOP 系统深度评测:从参数解析到 ROI 实战验证
大数据·人工智能·制造·精益工程
CAIE研习社3 小时前
从“会聊天”到能落地:光伏、风电运维里的AI怎么用?
大数据·人工智能