MapReduce中的MapJoin和ReduceJoin

ReduceJoin的体现是在Reduce阶段中就体现了,因为他是将不同数据块输出的相同的key2给聚合起来,所以只要Map阶段把不同的表或文件读取进来并做好区分就可以在reducer阶段进行ReduceJoin

Mapjoin不适合做大表Join,只适合用做小表,可以把小表提前放入缓冲中,在Map阶段进行join

示例代码:

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

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MapJoinMapper extends Mapper<LongWritable, Text, Text, Text>
{
	//把缓冲到表中部门放入到hashMap中
	 private Map<String, String> dept=new HashMap<>();

	//输出的是部门名称和员工姓名
	@Override
	protected void map(LongWritable key, Text value,Context context)
			throws IOException, InterruptedException {
		String[] split = value.toString().split(",");
		
		context.write(new Text(dept.get(split[7])), new Text(split[1]));
	}
	//读入缓冲的文件
	@SuppressWarnings("resource")
	@Override
	protected void setup(Context context)
			throws IOException, InterruptedException {
		//获取缓存的文件,因为我们只缓存了一个文件,所以只获取第一个就可以
		String path = context.getCacheFiles()[0].getPath();
		//然后从path中读入数据
		//获取读入缓冲的表明
		int indexOf = path.lastIndexOf("/");
		String deptName = path.substring(indexOf+1);
		//先创建一个文件输入流
		InputStream inputStream = new FileInputStream(deptName);
		InputStreamReader reader = new InputStreamReader(inputStream);
		BufferedReader br = new BufferedReader(reader);
		String line=null;
		while ((line=br.readLine())!=null)
		{
			String[] split = line.split(",");
			dept.put(split[0], split[1]);
		}
	}
	
	
}
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;

public class MapjoinMain
{
	public static void main(String[] args) throws Exception {
		//创建任务
		Job job = Job.getInstance(new Configuration());
		job.setJarByClass(MapjoinMain.class);
		//指定Map和map的输出类型
		job.setMapperClass(MapJoinMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		//将小表输入到内存中
		job.addCacheFile(new URI(args[0]));
		//指定输入输出类型
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		//指定文件输入路径和输出路径
		FileInputFormat.setInputPaths(job, new Path(args[1]));
		FileOutputFormat.setOutputPath(job, new Path(args[2]));
		//执行任务
		job.waitForCompletion(true);
	}
}

然后我们再介绍ReduceJoin的等值连接和自连接

它的原理是通过相同的键在Reduce阶段进行聚合,前提是需要做标记

我们先看一下等值连接,

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

枣泥馅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值