Mapreduce实现MapSideJoin

当连接的两个表是一个比较小的表和一个特别大的表的时候,我们把比较小的table直接放到内存中去,然后再对比较大的表格进行map操作。join就发生在map操作的时候,每当扫描一个大的table中的数据,就要去去查看小表的数据,哪条与之相符,继而进行连接。这里的join并不会涉及reduce操作。map端join的优势就是在于没有shuffle。
比如产品表很小,存储了几条产品信息,但是订单表却很庞大,相同的商品可以被很多人购买。

package com.bpf.mr.mapsidejoin;

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

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MapSideJoin {

    static class MapSideJoinMapper extends Mapper<LongWritable, Text, Text, NullWritable>{

        //用一个hashmap来保存产品信息表
        Map<String, String> pdInfoMap = new HashMap<String, String>();
        Text k = new Text();

        //setup方法是在maptask处理数据之前调用一次,可以用来做一些初始化工作
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("D:/测试数据/product.txt")));
            String line;
            while(StringUtils.isNotEmpty(line = reader.readLine())){
                String[] split = line.split(",");
                pdInfoMap.put(split[0], split[1]);

            }
            reader.close();
        }

        //由于已经持有完整的产品信息表,所以再map中就可以实现join(连接)逻辑 
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String orderLine = value.toString();
            String[] orders = orderLine.split(",");
            String pdName = pdInfoMap.get(orders[2]);
            k.set(orderLine + "," + pdName);
            context.write(k, NullWritable.get());

        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(MapSideJoin.class);

        job.setMapperClass(MapSideJoinMapper.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job, new Path("D:\\测试数据\\输入"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\测试数据\\输出"));

        //指定需要缓存一个文件到所有的maptask运行节点工作目录
        //job.addArchiveToClassPath(archive); 缓存jar包到task运行节点的classpath中
        //job.addCacheArchive(uri); 缓存压缩包文件到task运行节点的工作目录
        //job.addCacheFile(uri); 缓存普通文件到task运行节点的工作目录
        //job.addFileToClassPath(file); 缓存普通文件到task运行节点的classpath中

        //将产品表文件缓存到task工作节点的工作目录中去
        job.addCacheFile(new URI("file:/D:/测试数据/product.txt"));

        //map端join的逻辑不需要reduce
        job.setNumReduceTasks(0);

        boolean res = job.waitForCompletion(true);
        System.exit(res?0:1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值