1. hive的数据倾斜
介绍:只要在分布式一定有shuffle,避免不了出现数据倾斜,在混淆数据的过程中出现数据分布不均匀。比如:在MR编程中reducetask阶中的数据的大小不一致,即很多的数据集中到了一个reducetask中,hive的数据倾斜就是mapreduce的数据倾斜 maptask reducetask最后就是reducetask阶段的数据倾斜。
不会产生数据倾斜的场景:
- 不执行MapReduce程序,在hive中hive.fetch.task.conversion一共有三个可选值:
- none:表示所有的语句都执行MR,这个参数不可用
- minimal :表示select *、where 字段为分区字段、limit时不执行MR
- more :select 、filter /where 、limit不执行MR
- 聚合函数和group by一起使用的时候,在聚合函数和group by一起使用的时候,默认的MR底层在map端执行combiner,所以不会数据倾斜
会产生数据倾斜的场景:
- 聚合函数不和group by连用
- count(distinct)
- join 主要是reduce join会产生数据倾斜
具体场景分析:
1)join时null值过多:
以log日志为例,其中有一个字段为userid,但是userid的null值太多,在使用userid进行join时,所有的userid=null的数据都会到一个reduce中,这个reducetask数据量很大,就会产生数据倾斜。
解决方法1:
#null值不参与连接
select field1,field2,field3…
from log a left join user b on a.userid is not null and a.userid=b.
userid
union select field1,field2,field3 from log where userid is null;
解决方法2:
#将null值进行散列
select
*
from log a left join user b on
case when a.userid is null then concat("null",rand())
else a.userid end=b.userid;