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*
相关推荐
O***p6041 小时前
Java在分布式中的Archaius
java·开发语言·分布式
WordPress学习笔记2 小时前
专业建外贸网站公司推荐
大数据·前端·人工智能
Julian.zhou2 小时前
Anthropic破解长程任务难题:长期运行智能体的高效控制机制
大数据·人工智能
白日做梦Q5 小时前
Navicat for MySQL 详细使用指南:命令行操作与界面操作双视角全解析
大数据·mysql·adb·数据库开发
AI_56786 小时前
AI知识库如何重塑服务体验
大数据·人工智能
你好~每一天6 小时前
从传统行业到AI入门:我的CAIE Level I学习体验与思考
大数据·数据结构·人工智能·学习·jupyter·idea
G皮T6 小时前
【Elasticsearch】索引别名 aliases
大数据·elasticsearch·搜索引擎·es·索引·索引别名·aliases
wyiyiyi6 小时前
【数据结构+算法】非递归遍历二叉树的理解
大数据·数据结构·笔记·算法·leetcode·数据分析
爱跑步的程序员~7 小时前
Elasticsearch倒排索引
java·大数据·elasticsearch·搜索引擎·全文检索
专注API从业者7 小时前
构建分布式京东商品数据采集器:基于微服务的架构设计与实现
数据结构·数据库·分布式·微服务·架构