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

相关推荐
欧先生^_^20 分钟前
Spark 的一些典型应用场景及具体示例
大数据·分布式·spark
八股文领域大手子1 小时前
如何给GitHub项目提PR(踩坑记录
大数据·elasticsearch·github
爱吃龙利鱼1 小时前
elk中kibana一直处于可用和降级之间且es群集状态并没有问题的解决方法
大数据·elk·elasticsearch
腾讯云大数据1 小时前
腾讯云ES一站式RAG方案获信通院“开源大模型+软件创新应用”精选案例奖
大数据·elasticsearch·开源·云计算·腾讯云
苍煜1 小时前
Elasticsearch(ES)中的脚本(Script)
大数据·elasticsearch·搜索引擎
Hello kele2 小时前
解构与重构:“整体部分”视角下的软件开发思维范式
大数据·经验分享·程序员·重构·项目管理·人月神话·沟通困局
Elastic 中国社区官方博客2 小时前
使用 LangGraph 和 Elasticsearch 构建强大的 RAG 工作流
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
别这么骄傲3 小时前
Flink概念-状态一致性的三种级别
大数据·flink
和算法死磕到底3 小时前
ubantu18.04(Hadoop3.1.3)之Spark安装和编程实践
大数据·hadoop·pycharm·spark
菜鸟、上路3 小时前
Hadoop 集群扩容新增节点操作文档
大数据·hadoop·分布式