Reduce侧连接
1、reduce side join
在reduce端进行表的连接,该方法的特点就是操作简单,缺点是map端shffule后传递给reduce端的数据量过大,极大的降低了性能
连接方法:
(1)map端读入输入数据,以连接键为Key,待连接的内容为value,但是value需要添加特别的标识,表示的内容为表的表示,即若value来自于表1,则标识位设置为1,若来自表2,则设置为2,然后将map的内容输出到reduce
(2)reduce端接收来自map端shuffle后的结果,即<key, values>内容,然后遍历values,对每一个value进行处理,主要的处理过程是:判断每一个标志位,如果来自1表,则将value放置在特地为1表创建的数组之中,若来自2表,则将value放置在为2表创建的数组中,最后对两个数组进行求笛卡儿积,然后输出结果,即为最终表的连接结果。
2、map side join
在map端进行表的连接,对表的大小有要求,首先有一个表必须足够小,可以读入内存,另外的一个表很大,与reduce端连接比较,map端的连接,不会产生大量数据的传递,而是在map端连接完毕之后就进行输出,效率极大的提高
连接方法:
(1)首先要重写Mapper类下面的setup方法,因为这个方法是先于map方法执行的,将较小的表先读入到一个HashMap中。
(2)重写map函数,一行行读入大表的内容,逐一的与HashMap中的内容进行比较,若Key相同,则对数据进行格式化处理,然后直接输出。
实例与map侧连接一样,思路也与map侧连接一样,输出结果也一样。。。。。
package mapreduce01;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.LineReader;
public class Reduceduan {
static String INPUT_PATH = "hdfs://master:9000/qq/123";
static String OUTPUT_PATH="hdfs://master:9000/output";
static class MyMapper extends Mapper<Object,Object,Text,Text>{
Text output_key = new Text();
Text output_value = new Text();
protected void map(Object key,Object value,Context context) throws IOException,InterruptedException{
String[] tokens = value.toString().split(",");
if(tokens!=null&&tokens.length==2){
output_key.set(tokens[0]);
output_value.set(tokens[1]);
context.write(output_key,output_value);
}
}
}
static class MyReduce extends Reducer<Text,Text,Text,Text> {
Text output_key=new Text();
Text output_value=new Text();
Map<String,String> addMap = new HashMap<String,String>(); //image yingshe
protected void setup(Context context) throws java.io.IOException, java.lang.InterruptedException{
URI uri=context.getCacheFiles()[0];
Path path = new Path(uri);
FileSystem fs = path.getFileSystem(context.getConfiguration());
LineReader lineReader = new LineReader(fs.open(path));
Text line=new Text();
while(lineReader.readLine(line)>0){
String tokens[] = line.toString().split(",");
if(tokens!=null && tokens.length==2)
addMap.put(tokens[0], tokens[1]);
}
}
protected void reduce(Text key, Iterable<Text> values,Context context) throws IOException,InterruptedException{
if(values==null)
return
String addrName = addMap.get(values.iterator().next().toString());
output_value.set(addrName);
context.write(key,output_value);
}
}
public static void main(String[] args) throws Exception{
Path outputpath = new Path(OUTPUT_PATH);
Path cacheFile = new Path("hdfs://master:9000/qq/a");
Configuration conf = new Configuration();
FileSystem fs = outputpath.getFileSystem(conf);
if(fs.exists(outputpath)){
fs.delete(outputpath,true);
}
Job job=Job.getInstance(conf);
FileInputFormat.setInputPaths(job,INPUT_PATH);
FileOutputFormat.setOutputPath(job, outputpath);
URI uri =cacheFile.toUri();
job.setCacheFiles(new URI[]{uri}); //set cache address
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true);
}
}
Reduce侧连接的更多相关文章
- map侧连接
两个数据集中一个非常小,可以让小数据集存入缓存.在作业开始这些文件会被复制到运行task的节点上. 一开始,它的setup方法会检索缓存文件. 与reduce侧连接不同,Map侧连接需要等待参与连接的 ...
- MapReduce 示例:减少 Hadoop MapReduce 中的侧连接
摘要:在排序和reducer 阶段,reduce 侧连接过程会产生巨大的网络I/O 流量,在这个阶段,相同键的值被聚集在一起. 本文分享自华为云社区<MapReduce 示例:减少 Hadoop ...
- Hadoop的Map侧join
写了关于Hadoop下载地址的Map侧join 和Reduce的join,今天我们就来在看另外一种比较中立的Join. SemiJoin,一般称为半链接,其原理是在Map侧过滤掉了一些不需要join的 ...
- [Hadoop in Action] 第5章 高阶MapReduce
链接多个MapReduce作业 执行多个数据集的联结 生成Bloom filter 1.链接MapReduce作业 [顺序链接MapReduce作业] mapreduce-1 | mapr ...
- [大牛翻译系列]Hadoop(1)MapReduce 连接:重分区连接(Repartition join)
4.1 连接(Join) 连接是关系运算,可以用于合并关系(relation).对于数据库中的表连接操作,可能已经广为人知了.在MapReduce中,连接可以用于合并两个或多个数据集.例如,用户基本信 ...
- RxJava操作符(09-算术/聚合操作&连接操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51692493 本文出自:[openXu的博客] 目录: 算术聚合 Count Concat ...
- matlab基本指令
基本命令 close all //关闭所有figure 命令打开的窗口,在命令窗口输入 clear all //清除之前运行程序所存下的所有变量 size(mat) a = [1 2 3 ; 4 5 ...
- spark-初阶①(介绍+RDD)
spark-初阶①(介绍+RDD) Spark是什么? Apache Spark 是一个快速的, 多用途的集群计算系统, 相对于 Hadoop MapReduce 将中间结果保存在磁盘中, Spark ...
- Update(Stage4):spark_rdd算子:第2节 RDD_action算子_分区_缓存:算子和分区
一.reduce和reduceByKey: 二.:RDD 的算子总结 RDD 的算子大部分都会生成一些专用的 RDD map, flatMap, filter 等算子会生成 MapPartitions ...
随机推荐
- 19.Imagetragick 命令执行漏洞(CVE-2016–3714)
Imagetragick 命令执行漏洞(CVE-2016–3714) 漏洞简介: Imagetragick 命令执行漏洞在16年爆出来以后,wooyun上面也爆出了数个被该漏洞影响的大厂商,像腾讯, ...
- HDU 5241 Friends (大数)
题意:略. 析:答案就是32^n. 代码如下: import java.math.BigInteger; import java.util.Scanner; public class Main{ pu ...
- Entity Framework 分页处理
在SQL中进行分页,网上已经有很多例子了,在这里我使用Linq to SQL让C#来生成分页代码,首先创建分页的扩展方法: public static class Extensions { /// & ...
- 缓存处理类(MemoryCache结合文件缓存)
想提升站点的性能,于是增加了缓存,但是站点不会太大,于是不会到分布式memcached的缓存和redis这个nosql库,于是自己封装了.NET内置的缓存组件 原先使用System.Web.Cachi ...
- Java学习笔记(一)语法
基本语法 大小写敏感 类名:对于所有的类来说,类名的首字母应该大写 方法名:所有的方法名都应该以小写字母开头.如果方法名含有若干单词,则后面的每个单词首字母大写. 源文件名:源文件名必须和类名相同.当 ...
- C# 字符串转JSON
一.简单小结 C# 中 String 转 JSON var items = JsonConvert.DeserializeObject<class>(stringJSON); 这里的 cl ...
- heap 堆
实现了交换.向上维护,向下维护的原子功能,其它插入.删除.修改的功能应该不在话下. 于是有了代码:(luogu3378模板题) // luogu-judger-enable-o2 #include & ...
- android--系统路径获取
Environment 常用方法: * 方法:getDataDirectory()解释:返回 File ,获取 Android 数据目录.* 方法:getDownloadCacheDirectory( ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- STP-15-PortFast端口
PortFast是大家熟知的传统STP和PVST+改进特性,它也是RSTP和MST中标准化的增强特性.实质上,它定义了个一个边界端口.边界端口在启用之后立刻进入转发状态,不产生拓扑变化事件,不会因为处 ...