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.查看

相关推荐
赵谨言10 小时前
基于python大数据的城市扬尘数宇化监控系统的设计与开发
大数据·开发语言·经验分享·python
程序员小羊!11 小时前
Flink状态编程之算子状态(OperatorState)
大数据·flink
TaoSense11 小时前
Milvus向量数据库介绍
大数据·人工智能
智海观潮12 小时前
聊聊Spark的分区
java·大数据·spark
洛克大航海12 小时前
集群环境安装与部署 Hadoop
大数据·hadoop·ubuntu·集群部署 hadoop
EasyCVR13 小时前
赋能智慧水利:视频汇聚平台EasyCVR智慧水利工程视频管理系统解决方案
大数据
程序员洲洲13 小时前
使用亮数据爬虫API一键式爬取Facebook数据
大数据·数据·亮数据·bright data·爬虫api
汽车仪器仪表相关领域14 小时前
工业商业安全 “哨兵”:GT-NHVR-20-A1 点型可燃气体探测器实操解析与场景适配
大数据·人工智能·功能测试·安全·安全性测试
ctrigger14 小时前
电子信息三胞胎:电子信息工程、电子科学技术、电子信息科学技术
大数据·注册电气工程师
得物技术15 小时前
告别数据无序:得物数据研发与管理平台的破局之路
大数据·数据库·数据分析