1、数据倾斜的原因
1.1 原因
发生倾斜的根本原因在于,shuffle之后,key分布的数据量不均匀,使得大量的key集中在某个reduce节点,导致此节点过于“忙碌”,在其他节点都处理完之后,任务的结整需要等待此节点处理完,使得整个任务被此节点堵塞。
要解决此问题,主要可以分为两大块:一是尽量不shuffle;二是shuffle之后,在reduce节点上的key分布尽量均匀。
从解决方案来看,又分为两大块:一是分为参数调节优化;二是sql语句优化
1.2 表现
任务进度长时间维持在99%(或100%),查看任务监控页面,发现只有少量(1个或几个)reduce子任务未完成。因为其处理的数据量和其他reduce差异过大。
单一reduce的记录数与平均记录数差异过大,通常可能达到3倍甚至更多。 最长时长远大于平均时长。
2、数据倾斜的解决方案
2.1 参数优化
2.1.1 group by
(1)参数优化:设置负载均衡
set hive.map.aggr = true #是否在map端进行聚合,默认为true
set hive.groupby.skewindata = true #当使用group by 有数据倾斜的时候 进行负载均衡
select
key
, count(1) as cnt
from tb_name
group by key;
加参数后,hive在数据倾斜的时候会进行负载均衡,控制生成两段MR Job
第一个 MR Job 中,Map 的输出结果集合会随机分布到 Reduce 中,每个 Reduce 做部分聚合操作,这样处理的结果是相同的 Group By Key 有可能被分发到不同的 Reduce 中,从而达到负载均衡的目的;
第二个 MR Job 再根据预处理的数据结果按照 Group By Key 分布到 Reduce 中(这个过程可以保证相同的 Group By Key 被分布到同一个 Reduce 中),最后完成最终的聚合操作。
(2)语法优化:在底层增加随机数先分成多个reducce
select a.key, sum(cnt) as cnt
from (
select key
, if(key = 'key001',random(),0)
, count(1) as cnt
from tb_name
group by key,if(key = 'key001',random(),0)
) t
group by t.key;
2.2 SQL语句优化
2.2.1 Join
①小表Join大表:mapjoin
解决方法:设置参数转换为mapjoin,mapjoin将小表放入内存,在map端和大表逐一匹配,省去reduce操作。
select /* +mapjoin(a) */ -- 多个 /* +mapjoin(a,c) */ a可以是一个子查询,可以是左表也可以是右表
a.id , a.name, b.age
from a
left join b
on a.id = b.id;
原理是将所有的小表全量复制到每个map任务节点,然后再将小表缓存在每个map节点的内存里与大表进行join工作。在Map阶段完成join操作,这避免了 Shuffle。
②大表Join大表
遇到需要进行join的但是关联字段有数据为null、0、99等,如表一的id需要和表二的id进行关联,null值的reduce就会落到一个节点上
解决方法:把空值的 key 变成一个字符串加上随机数(case when string+rand()),就能把倾斜的数据分到不同的reduce上
select *
from log a
left outer join users b
on case when a.user_id is null then concat(‘hive’,rand() ) else a.user_id end = b.user_id;
-- on coalesce(column, concat('hive',rand()) = b.user_id; -- 结果同上
③join字段类型不同
用户表中user_id字段为int,log表中user_id字段既有string类型也有int类型。当按照user_id进行两个表的Join操作时,
默认的Hash操作会按int型的id来进行分配,这样会导致所有string类型id的记录都分配到一个Reducer中。
解决方法:直接把int类型都转为string就好了,这样key字段都为string,hash时就按照string类型分配了
select * from users a
left outer join logs b
on a.usr_id = cast(b.user_id as string)
2.2.2 count(distinct)
案例一
-- 优化前
select count(distinct a) from t;
-- 优化后
select sum(1) from (select a from t group by a) ;
案例二
-- 优化前
SELECT prod_type
,count(DISTINCT IF(num_alipay_1days>0,user_id))AS cat_users_1days
,count(DISTINCT IF(num_alipay_7days>0,user_id))AS cat_users_7days
,count(DISTINCT IF(num_alipay_30days>0,user_id))AS cat_users_30days
FROM a
WHERE dt = '${bizdate}'
group by prod_type
;
-- 优化后
select prod_type
,sum(users_1days)
,sum(users_7days)
,sum(users_30days)
FROM
(
SELECT prod_type
,user_id
,max(if(num_alipay_1days>0,1,0)) as users_1days
,max(if(num_alipay_7days>0,1,0)) as users_7days
,max(if(num_alipay_30days>0,1,0)) as users_30days
FROM a
WHERE dt = '${bizdate}'
group by prod_type
,user_id
)t
group by prod_type
;