hive.map.aggr=true;在map中会做部分聚集操作,效率更高但需要更多的内存。
hive.groupby.skewindata=true:数据倾斜时负载均衡,当选项设定为true,生成的查询计划会有两个MRJob。第一个MRJob 中,
Map的输出结果集合会随机分布到Reduce中,每个Reduce做部分聚合操作,并输出结果,这样处理的结果是相同的GroupBy Key
有可能被分发到不同的Reduce中,从而达到负载均衡的目的;第二个MRJob再根据预处理的数据结果按照GroupBy Key分布到
Reduce中(这个过程可以保证相同的GroupBy Key被分布到同一个Reduce中),最后完成最终的聚合操作。
但是两者组合使用,计算pv时候会出现错误,现象如下四个sql
前3个看问题,最后一个改进
一
set hive.groupby.skewindata=false ;
set hive.map.aggr = true;
select dt,’整体’ as os,count(1) ,count(distinct device_id)
from ods_cre_news_click
where ‘0401’<=dt and dt<=’0418′ and pagetype=’tianyi_nvideo’ and channel=’news_video’ and event_method !=’Android_Lite’
group by dt
0401 整体 394384 88374
0402 整体 350275 83083
0403 整体 362113 84762
0410 整体 355632 84843
0404 整体 362981 86480
0411 整体 371531 95118
0405 整体 407410 92178
0412 整体 419627 108835
0406 整体 386203 88369
0413 整体 456934 114080
0407 整体 381992 87484
0414 整体 480010 115757
0408 整体 366030 86030
0415 整体 459180 111333
0409 整体 348939 82524
0416 整体 418914 105665
0417 整体 430969 105115
0418 整体 426462 102444
二
set hive.groupby.skewindata=true ;
set hive.map.aggr = false;
select dt,’整体’ as os,count(1) ,count(distinct device_id)
from ods_cre_news_click
where ‘0401’<=dt and dt<=’0418′ and pagetype=’tianyi_nvideo’ and channel=’news_video’ and event_method !=’Android_Lite’
group by dt
0401 整体 394384 88374
0402 整体 350275 83083
0403 整体 362113 84762
0410 整体 355632 84843
0404 整体 362981 86480
0411 整体 371531 95118
0405 整体 407410 92178
0412 整体 419627 108835
0406 整体 386203 88369
0413 整体 456934 114080
0407 整体 381992 87484
0414 整体 480010 115757
0408 整体 366030 86030
0415 整体 459180 111333
0409 整体 348939 82524
0416 整体 418914 105665
0417 整体 430969 105115
0418 整体 426462 102444
三
set hive.groupby.skewindata=true ;
set hive.map.aggr = true;
select dt,’整体’ as os,count(1) ,count(distinct device_id)
from ods_cre_news_click
where ‘0401’<=dt and dt<=’0418′ and pagetype=’tianyi_nvideo’ and channel=’news_video’ and event_method !=’Android_Lite’
group by dt
0401 整体 267833 88374
0402 整体 246562 83083
0403 整体 246095 84762
0410 整体 242038 84843
0404 整体 253287 86480
0411 整体 265535 95118
0405 整体 279676 92178
0412 整体 298332 108835
0406 整体 273046 88369
0413 整体 325518 114080
0407 整体 257079 87484
0414 整体 324205 115757
0408 整体 253223 86030
0415 整体 319272 111333
0409 整体 248462 82524
0416 整体 281923 105665
0417 整体 289144 105115
0418 整体 280593 102444
四:改进,把count改为sum
set hive.groupby.skewindata=true ;
set hive.map.aggr = true;
select dt,’整体’ as os,sum(1) ,count(distinct device_id)
from ods_cre_news_click
where ‘0401’<=dt and dt<=’0418′ and pagetype=’tianyi_nvideo’ and channel=’news_video’ and event_method !=’Android_Lite’
group by dt
0401 整体 394384 88374
0402 整体 350275 83083
0403 整体 362113 84762
0410 整体 355632 84843
0404 整体 362981 86480
0411 整体 371531 95118
0405 整体 407410 92178
0412 整体 419627 108835
0406 整体 386203 88369
0413 整体 456934 114080
0407 整体 381992 87484
0414 整体 480010 115757
0408 整体 366030 86030
0415 整体 459180 111333
0409 整体 348939 82524
0416 整体 418914 105665
0417 整体 430969 105115
0418 整体 426462 102444