HIVE: Map Join Vs Common Join, and SMB
HIVE
Map Join is nothing but the extended version of Hash Join of SQL Server - just extending Hash Join into Distributed System.
SMB(Sort Merge Bucket) Join is also similar to the SQL Server Merge Join mechnism - just extending it into Distributed System.
If the tables being joined are bucketized, and the buckets are a multiple of each other, the buckets can be joined with each other. If table A has 8 buckets are table B has 4 buckets, the following join:
can be done on the mapper only. Instead of fetching B completely for each mapper of A, only the required buckets are fetched. For the query above, the mapper processing bucket 1 for A will only fetch bucket 1 of B. It is not the default behavior, and is governed by the following parameter
set hive.optimize.bucketmapjoin = true
If the tables being joined are sorted and bucketized, and the number of buckets are same, a sort-merge join can be performed. The corresponding buckets are joined with each other at the mapper. If both A and B have 4 buckets
can be done on the mapper only. The mapper for the bucket for A will traverse the corresponding bucket for B. This is not the default behavior, and the following parameters need to be set:
下面进行一次简单的性能比较测试.
表结构:
hive> desc student;
OK
no double
name string
code string
Time taken: 0.568 seconds, Fetched: row(s)
hive> desc stu_add;
OK
add_code double
address string
Time taken: 0.093 seconds, Fetched: row(s)
表student大小,约470M
-rwxr-xr-x stevenxia supergroup -- : /user/hive/warehouse/student/part-m-00000_copy_7
表stu_add小大约 1K
Found items
-rwxr-xr-x stevenxia supergroup -- : /user/hive/warehouse/stu_add/part-m-
运行
select s.name, a.address from student s join stu_add a on s.no = a.add_code;
进行了多次测试,结果:
序号 | set hive.auto.convert.join = false; | set hive.auto.convert.join = true; |
1 | 2m 1s | 35s |
2 | 2m 9s | 33s |
3 | 2m 1s | 33s |
WHY?
我想主要Common Join有两点性能消耗比较多:
a. Shuffle过程,需要把各个mapper的结果写到磁盘
b. 需要把map task的结果复制到其它data node上进行reduce
这是我的理解,如有错误,不吝赐教。
reduce side join是一种最简单的join方式,其主要思想如下:
在map阶段,map函数同时读取两个文件File1和File2,为了区分两种来源的key/value数据对,对每条数据打一个标签(tag),比如:tag=0表示来自文件File1,tag=2表示来自文件File2。即:map阶段的主要任务是对不同文件中的数据打标签。
在reduce阶段,reduce函数获取key相同的来自File1和File2文件的value list, 然后对于同一个key,对File1和File2中的数据进行join(笛卡尔乘积)。即:reduce阶段进行实际的连接操作。
之所以存在reduce side join,是因为在map阶段不能获取所有需要的join字段,即:同一个key对应的字段可能位于不同map中。Reduce side join是非常低效的,因为shuffle阶段要进行大量的数据传输。
Map side join是针对以下场景进行的优化:两个待连接表中,有一个表非常大,而另一个表非常小,以至于小表可以直接存放到内存中。这样,我们可以将小表复制多份,让每个map task内存中存在一份(比如存放到hash table中),然后只扫描大表:对于大表中的每一条记录key/value,在hash table中查找是否有相同的key的记录,如果有,则连接后输出即可。
为了支持文件的复制,Hadoop提供了一个类DistributedCache,使用该类的方法如下:
(1)用户使用静态方法DistributedCache.addCacheFile()指定要复制的文件,它的参数是文件的URI(如果是HDFS上的文件,可以这样:hdfs://namenode:9000/home/XXX/file,其中9000是自己配置的NameNode端口号)。JobTracker在作业启动之前会获取这个URI列表,并将相应的文件拷贝到各个TaskTracker的本地磁盘上。(2)用户使用DistributedCache.getLocalCacheFiles()方法获取文件目录,并使用标准的文件读写API读取相应的文件。
reduce side join + BloomFilter
将小表中的key保存到BloomFilter中,在map阶段扫描过滤大表,可能有一些不在小表中的记录没有过滤掉(但是在小表中的记录一定不会过滤掉),这没关系,只不过增加了少量的网络IO而已.
Semi Join,也叫半连接,是从分布式数据库中借鉴过来的方法。它的产生动机是:对于reduce side join,跨机器的数据传输量非常大,这成了join操作的一个瓶颈,如果能够在map端过滤掉不会参加join操作的数据,则可以大大节省网络IO.
实现方法很简单:选取一个小表,假设是File1,将其参与join的key抽取出来,保存到文件File3中,File3文件一般很小,可以放到内存中。在map阶段,使用DistributedCache将File3复制到各个TaskTracker上,然后将File2中不在File3中的key对应的记录过滤掉,剩下的reduce阶段的工作与reduce side join相同。
Sort Merge Bucket Join 存在的目的主要是为了解决大表与大表间的 Join 问题,分桶其实就是把大表化成了“小表”,然后 Map-Side Join 解决之,这是典型的分而治之的思想。
连接两个在(包含连接列的)相同列上划分了桶的表,可以使用 Map 端连接 (Map-side join)高效的实现。比如JOIN操作。对于JOIN操作两个表有一个相同的列,如果对这两个表都进行了桶操作。那么将保存相同列值的桶进行JOIN操作就可以,可以大大较少JOIN的数据量.
set hive.input.format=org.apache.hadoop.hive.ql.io.BucketizedHiveInputFormat;
set hive.optimize.bucketmapjoin = true;
set hive.optimize.bucketmapjoin.sortedmerge = true;
HIVE: Map Join Vs Common Join, and SMB的更多相关文章
- Hive中的4种Join方式
common join 普通join,性能较差,存在Shuffle map join 适用情况:大表join小表时,做不等值join 原理:将小表数据广播到各个节点,存储在内存中,在map阶段直接jo ...
- 061 hive中的三种join与数据倾斜
一:hive中的三种join 1.map join 应用场景:小表join大表 一:设置mapjoin的方式: )如果有一张表是小表,小表将自动执行map join. 默认是true. <pro ...
- Hive Essential (4):DML-project,filter,join,union
1. Project data with SELECT The most common use case for Hive is to query data in Hadoop. To achieve ...
- Hive 中的 LEFT SEMI JOIN 与 JOIN ON
hive 的 join 类型有好几种,其实都是把 MR 中的几种方式都封装实现了,其中 join on.left semi join 算是里边具有代表性,且使用频率较高的 join 方式. 1.联系 ...
- Hive 中Join的专题---Join详解
1.什么是等值连接? 2.hive转换多表join时,如果每个表在join字句中,使用的都是同一个列,该如何处理? 3.LEFT,RIGHT,FULL OUTER连接的作用是什么? 4.LEFT或RI ...
- HIVE中join、semi join、outer join
补充说明 left outer join where is not null与left semi join的联系与区别:两者均可实现exists in操作,不同的是,前者允许右表的字段在select或 ...
- 关于Hive中的join和left join的理解
一.join与left join的全称 JOIN是INNER JOIN的简写,LEFT JOIN是LEFT OUTER JOIN的简写. 二.join与left join的应用场景 JOIN一般用于A ...
- flink-----实时项目---day06-------1. 获取窗口迟到的数据 2.双流join(inner join和left join(有点小问题)) 3 订单Join案例(订单数据接入到kafka,订单数据的join实现,订单数据和迟到数据join的实现)
1. 获取窗口迟到的数据 主要流程就是给迟到的数据打上标签,然后使用相应窗口流的实例调用sideOutputLateData(lateDataTag),从而获得窗口迟到的数据,进而进行相关的计算,具体 ...
- SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结
在SQL语言中,存在着各种Join,有Left Join, Right Join, Inner Join, and Natural Join等,对于初学者来说肯定一头雾水,都是神马跟神马啊,它们之间到 ...
随机推荐
- 喜大普奔!Fanvas正式对外开源了,一键把Flash转为Canvas动画!移动终端动画开发不再困难。
http://code.tencent.com/ https://github.com/TencentOpen/Fanvas DEMO: http://kenkozheng.github.io/fan ...
- 在Unicode版Inno Setup中使用ISSkin
ISSkin是Code jock 公司出品的Inno Setup 皮肤插件,用于为Inno制作的安装程序提供皮肤功能. 自Delphi发布2009之后,Inno Setup 开始出现支持Unicode ...
- GO語言基礎教程:流程控制
在開始一個新的章節之前先來回顧上一篇文章的部份,首先我們來看這段代碼: package main import ( "fmt" ) func main(){ var x,y int ...
- redolog文件头简单探究
先切换: SQL> select group#,status from v$log; GROUP# STATUS---------- ---------------- 1 INA ...
- 用SDWebImage渐变加载图片
用SDWebImage渐变加载图片 使用 使用请详细查看源码,只需要给定一个图片地址以及一个placeHolder图片(非必须)即可. 效果 源码 https://github.com/YouXian ...
- Spring 整合 Flex (BlazeDS)无法从as对象 到 Java对象转换的异常:org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Date' to required type 'java.sql.Timestamp' for property 'wfsj'; nested exception is java.lang.Ill
异常信息如下: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value ...
- Spring+hibernate+struts
一.Spring 主要功能:解耦和(对象之间可配置,依赖注入的) 1.概念: 容器:容器可以装载对象,实例化对象,配置对象之间的依赖关系. IOC/DIIOC:Inversion of Control ...
- madown标签说明
1.删除线的用法 ~~这是删除线~~
- 《STL系列》之vector原理及实现
最近忙得蛋疼,但还是想写点属于自己的东西.也不知道写点啥,最后决定试着自己实现STL中常用的几个集合,一来加深自己对STL的理解,二来看看自己是否有这个能力实现.实现目标就是:1能和STL兼容:2最大 ...
- VMware安装RedHat Linux虚拟机图文详解
创建Red Hat Linux虚拟机 1.打开VMware,开始创建虚拟机 点击菜单[文件]->[新建虚拟机]. 2.默认典型,单击[下一步] 3.选择安装来源 在这里,我们选择安装来源为[安装 ...