场景
有木有发现工作中偶尔有些大量的null值或者一些无意义的数据参与到计算作业中,任务跑的贼慢,表中有大量的null值,如果表之间进行join关联操作,就会有shuffle产生,这样所有的null值都会集中在一个reduce中,会产生数据倾斜,降低作业效率。辣么我们该如何避免这种囧况呢,现在给大家唠唠
方案一、避免 null 值参与关联
手动过滤null 值不进行 join,,值将非 null 值得数据进行关联,这样就避免了null 值参与运算;
select a.log_info, b.user_name from temp.jc_shop_info a left join temp.jc_f_user_info b on a.user_id is not null and a.user_id = b.user_id union all select log_info, null user_name from temp.jc_shop_info where user_id is null;
方案二 随机赋值
因为null值参join 也无法关联到数据,那么我们可以给null值随机赋值,这样它们的hash结果就不一样,随机数均匀的进到不同的reduce中从而避免数据倾斜:
select a.log_info, b.user_name from (select *, if(user_id is null, concat('user__-', rand())) user_id_new from temp.jc_shop_info) a left join temp.jc_f_user_info b on user_id_new = b.user_id;
小结
今天介绍了在hivesql中如何解决null值引发的数据倾斜问题,其核心思路两个,一是null值单独过滤出不参与匹配,非null值的进行join匹配;二是随机思想,将容易倾斜的key随机打散分到不同的redcue中而不是同一个reduce中。这两种思想是解决此类问题的核心思想