Mapreduce实现MapSideJoin
来源:互联网 发布:备案域名交易平台top 编辑:程序博客网 时间:2024/06/04 23:30
当连接的两个表是一个比较小的表和一个特别大的表的时候,我们把比较小的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); }}
阅读全文
0 0
- Mapreduce实现MapSideJoin
- MapReduce实现流程分析
- 用MongoDB实现MapReduce
- Erlang里实现MapReduce
- MapReduce Join联结实现
- Mapreduce pairs算法 实现
- Mapreduce Strips算法 实现
- MapReduce之Join实现
- Erlang里实现MapReduce
- 用MongoDB实现MapReduce
- 用MongoDB实现MapReduce
- Erlang里实现MapReduce
- C# MapReduce 实现
- MapReduce实现join操作
- 用MongoDB实现MapReduce
- MapReduce 简单实现统计
- MapReduce Join联结实现
- MapReduce Join联结实现
- Spring 4.2.2以上版本和swagger集成方案和踩过的坑
- 遇到版本号判断的一个原则
- 第一个安卓驱动程序
- Vue中使用vux的配置,分为两种情况:
- Windows Server2008 R2安装Mysql5.7.19时遇到问题:
- Mapreduce实现MapSideJoin
- Word Count (Map Reduce)
- Kotlin 一统天下?Kotlin/Native 开始支持 iOS 和 Web 开发
- Kaldi HMM拓扑和状态转换模型
- VueJs 监听 window.resize 方法---窗口变化
- WAP_WSP的Well-known header
- 优雅的QSignleton (一) Singleton单例实现
- MySQL学习(二)【MySQL数据库对象与应用】
- sigaction-sigqueue