MapReduce-day05-第三章框架原理-3.6MapJoin机制

本文介绍了如何使用MapJoin优化Hadoop MapReduce任务,避免大量数据在Reducer阶段进行Join导致的效率低下问题。通过在Map阶段加载缓存文件并处理,可以显著提高处理速度。示例代码展示了MapJoinDriver和MapJoinMapper的实现,包括配置缓存文件、设置Map输出和最终输出类型、跳过Reduce阶段以及Mapper中读取缓存数据并执行Join操作的逻辑。
摘要由CSDN通过智能技术生成

因为Map阶段文件量可能很大,如果都是放到Reducer阶段进行Join的话,可能会造成处理效率低下的现象。所以尽可能采用MapJoin,同时要注意缓存文件的问题。

 

package com.atguigu.mapreduce.mapjoin;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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.output.FileOutputFormat;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class MapJoinDriver {
    public static void main(String[] args) throws IOException, URISyntaxException, ClassNotFoundException, InterruptedException {

        // 1 获取job 信息
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 2 设置加载jar 包路径
        job.setJarByClass(MapJoinDriver.class);

        // 3 关联mapper
        job.setMapperClass(MapJoinMapper.class);

        // 4 设置Map 输出KV 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        // 5 设置最终输出 KV 类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        // 加载缓存数据
        job.addCacheFile(new URI("file:///F:/input/inputtable/pd.txt"));
                // Map 端 Join 的逻辑不需要 Reduce 阶段,设置 reduceTask 数量为 0
         job.setNumReduceTasks(0);

                // 6 设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path("F:\\input\\inputtable2"));
        FileOutputFormat.setOutputPath(job, new Path("F:\\output16"));

        // 7 提交
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);

    }

}


package com.atguigu.mapreduce.mapjoin;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;

public class MapJoinMapper extends Mapper<LongWritable , Text ,Text , NullWritable> {


    private HashMap<String, String> pdMap = new HashMap<>();
    private Text outK = new Text();

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        //获取缓存的文件,并把文件内容封装到集合中pd.txt
        URI[] cacheFiles = context.getCacheFiles();

        FileSystem fs = FileSystem.get(context.getConfiguration());
        FSDataInputStream fis = fs.open(new Path(cacheFiles[0]));

        //从流里面读取数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));

        String line;
        while (StringUtils.isNotEmpty(line = reader.readLine())){
            // 切割
            String[] fields = line.split("\t");

            // 赋值
            pdMap.put(fields[0],fields[1]);
        }

        IOUtils.closeStream(reader);
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //处理order.txt
        String line = value.toString();
        String[] fidlds = line.split("\t");

        //获取pid
        String pname = pdMap.get(fidlds[1]);

        //获取订单id和订单数量,封装
        outK.set(fidlds[0] + "\t"+pname+"\t"+fidlds[2]);

        context.write(outK,NullWritable.get());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

总会有天明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值