Hive数据倾斜方法小结
一、含义
-
现象:在执行HiveQL或MapReduce作业时,卡在map100%,reduce99%;
-
本质:进行分布式计算时,某些节点的计算能力比较强或者需要计算的数据比较少,很快执行完毕后,必须等待其他未完成节点。
二、原因及解决方法
-
group by 场景:
方法:set hive.map.aggr=true;在map端部分聚合
hive.groupby.skwindata=true; 生成的查询计划会有两个 MR Job
第一个 MR Job 中,Map 的输出结果集合会随机分布到 Reduce 中,每个 Reduce 做部分聚合操作,并输出结果,相同的 Group By Key 有可能被分发到不同的 Reduce 中,从而达到负载均衡;
第二个 MR Job 再根据预处理的数据结果按照 Group By Key 分布到 Reduce 中(这个过程可以保证相同的 Group By Key 被分布到同一个 Reduce 中),最后完成最终的聚合操作。
-
HiveQL中包含count(distinct):
若数据量很大,执行select a,count(distinct b) from t group by a;
方法:使用sum...group by代替。
如:select a,sum(1) from (select a, b from t group by a,b) group by a;
-
一个大表和一个小表进行join操作:
方法:使用mapjoin 将小表加载到内存中;
如:select /*+ MAPJOIN(a) */
a.c1, b.c1 ,b.c2
from a join b
where a.c1 = b.c1;
http://mp.weixin.qq.com/s?src=11×tamp=1635426391&ver=3402&signature=KCDxD0ysa8FgnN9PCM5tDvd6RHlsjrfBZswFJz6GMdS-82fnpyJUsWFEMXXQzE9KxJtUbz46W6pZEYq18svYpp18H03HGJryRqH4ZEUkPgOvcUZaBaG1r-7IA3pc4oT*&new=1
-
进行join时关联字段有数据为空:
方法1:id为空的不参与关联;
方法2:给空值分配随机的key值;
如:select * from log a
left join users b
on
case when a.user_id is null
then concat(‘hive’,rand() )
else a.user_id end = b.user_id;