MapReduce

1.需求

创建一个文件上传到HDFS,统计每个学生的总成绩,文件内容如下:

使用MapReduce

张三 英语 80 河南省

张三 数学 50 河南省

张三 语文 60 河南省

李四 英语 90 河南省

李四 语文 90 河南省

李四 数学 85 河南省

通过结果:

张三 190

李四 265

2.上传到hdfs

3.IDEA代码

添加依赖

java 复制代码
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-app</artifactId>
            <version>3.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-yarn-server-resourcemanager</artifactId>
            <version>3.1.4</version>
        </dependency>
    </dependencies>

java 复制代码
package com.yh;

import org.apache.hadoop.conf.Configuration;
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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

// 测试类

public class ScoreTest {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");

        Job job = Job.getInstance(conf);
        job.setJarByClass(ScoreTest.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        TextInputFormat.addInputPath(job,new Path("/score.txt"));
        TextOutputFormat.setOutputPath(job,new Path("/out4"));

        job.setMapperClass(ScoreMapper.class);
        job.setReducerClass(ScoreReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        boolean b = job.waitForCompletion(true);
        System.out.println(b);


    }
    
    // Mapper类

    static class ScoreMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
       private Text student = new Text();
       private IntWritable score = new IntWritable();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] parts = value.toString().split("\\s+"); // 假设字段由空白字符分隔
            if (parts.length >= 4) {
                student.set(parts[0]);
                score.set(Integer.parseInt(parts[2]));
                context.write(student, score);
            }
        }
    }


//Reducer类

    static class ScoreReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values){
                sum = sum+value.get();
            }
            context.write(key,new IntWritable(sum));

        }
    }
}

4.maven打包

5.上传

6.查看

相关推荐
朗心心理6 小时前
朗心科技:以数智化引领心理健康服务新标杆
大数据·人工智能·科技·心理健康·朗心科技·数智化心理育人·一站式心理中心建设
无忧智库7 小时前
破局与重构:大型集团化协同管理平台的全景式深度解构(PPT)
大数据
码云数智-大飞8 小时前
进程、线程与协程:并发模型的演进与 Go 语言的 GMP 革命
大数据
XiaoMu_0019 小时前
基于大数据的糖尿病数据分析可视化
大数据·数据挖掘·数据分析
阿里云大数据AI技术9 小时前
Celeborn 如何让 EMR Serverless Spark 的 Shuffle 舒心、放心、安心
大数据·spark
AI营销快线10 小时前
AI营销获客难?原圈科技深度解析SaaS系统增长之道
大数据·人工智能
星幻元宇VR10 小时前
VR环保学习机|科技助力绿色教育新模式
大数据·科技·学习·安全·vr·虚拟现实
CryptoPP11 小时前
开发者指南:构建实时期货黄金数据监控系统
大数据·数据结构·笔记·金融·区块链
ZGi.ai12 小时前
生产级 Agent 编排 从单一 LLM 调用到多智能体工作流的工程设计
大数据·数据库·人工智能
天远数科12 小时前
分布式系统实战:基于天远二手车估值API构建高可用车辆估值微服务
大数据·微服务·云原生·架构