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

相关推荐
Lill_bin10 小时前
深入理解ElasticSearch集群:架构、高可用性与数据一致性
大数据·分布式·elasticsearch·搜索引擎·zookeeper·架构·全文检索
涛思数据(TDengine)11 小时前
TDengine 与 SCADA 强强联合:提升工业数据管理的效率与精准
大数据·时序数据库·tdengine
isNotNullX12 小时前
如何用SQL Server和Oracle进行数据同步?
大数据·数据库·sql·oracle
RwTo13 小时前
Elasticsearch 聚合搜索
大数据·elasticsearch·搜索引擎·全文检索
isNotNullX13 小时前
HBase在大数据实时处理中的角色
大数据·数据库·hbase
白总Server13 小时前
MySQL在大数据场景应用
大数据·开发语言·数据库·后端·mysql·golang·php
求学小火龙14 小时前
ElasticSearch介绍+使用
java·大数据·elasticsearch
檀越剑指大厂14 小时前
【Elasticsearch系列六】系统命令API
大数据·elasticsearch·搜索引擎
数据运营新视界14 小时前
你知道企业架构中核心的4大架构联系和不同吗?
大数据·架构
h1771134720514 小时前
基于区块链的相亲交易系统源码解析
大数据·人工智能·安全·系统架构·交友