MapJoin与ReduceJoin实例

产品表:
在这里插入图片描述
订单表:
在这里插入图片描述
需求:通过产品表和订单表得到,数据为(2019 01 小米 1)的表。

一、MapJoin

MapJoin适合小表+大表的联合。

1、Map类直接进行join操作

package com.join.mapjoin;

import org.apache.commons.lang.StringUtils;
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.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

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

    //定义HashMap对象存储小表对象
    HashMap hashMap = new HashMap<String, String>();

    /**
     * 将小表加载到内存中
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
   
        //获取缓存的文件
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\Bigdata\\product.txt"), "UTF-8"));
        //按行读取数据
        String line;
        while (StringUtils.isNotEmpty(line=reader.readLine())){
   
            //切分小表行数据
            String[] split = line.split("\t");
            //数据存储进HashMap集合
            hashMap.put(split[0], split[1]);
        }
    }

    /**
     * Map方法
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
   
        //获取大表数据
        String line = value.toString();
        //切分
        String[] fields = line.split("\t");
        //按照productId关联
        String productId = fields[1];
        if (hashMap.containsKey(productId)) {
   
            context.write(new Text(fields[0].substring(0, 4) + "\t" + fields[0].substring(4)
                    + "\t" + hashMap.get(productId) + "\t" + fields[2]), NullWritable.get());
        }
    }

}

2、驱动程序

package com.join.mapjoin;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值