hive使用技巧(三)——巧用group by实现去重统计

相关文章推荐:

hive使用技巧(一)自动化动态分配表分区及修改hive表字段名称
hive使用技巧(二)——共享中间结果集
hive使用技巧(三)——巧用group by实现去重统计

hive使用技巧(四)——巧用MapJoin解决数据倾斜问题

Hive使用技巧(五)—— 一行转多行,多行转一行

网站统计中常用的指标,pv ,uv , 独立IP,登录用户等,都涉及去重操作。全年的统计,PV超过100亿以上。即使是简单的去重统计也非常困难。


1、统计去重,原来SQL如下

[sql]  view plain  copy
  1. select substr(day,1,4) year,count(*) PV,count(distinct cookieid) UV,count(distinct ip) IP,count(distinct userid) LOGIN   
  2. from dms.tracklog_5min a    
  3. where substr(day,1,4)='2015'  
  4. group by substr(day,1,4)  
  5. ;  

统计中四个指示,三个都涉及了去重,任务跑了几个小时都未出结果。


2、利用group by 实现去重

[sql]  view plain  copy
  1. select "2015","PV",count(*) from dms.tracklog_5min  
  2. where day>='2015' and day<'2016'  
  3. union all   
  4. select "201505","UV",count(*) from (  
  5. select  cookieid from dms.tracklog_5min  
  6. where day>='2015' and day<'2016'  group by cookieid ) a   
  7. union all   
  8. select "2015","IP",count(*) from (  
  9. select  ip from dms.tracklog_5min  
  10. where day>='2015' and day<'2016'  group by ip ) a   
  11. union all   
  12. select "2015","LOGIN",count(*) from (  
  13. select  userid from dms.tracklog_5min  
  14. where day>='2015' and day<'2016' group by userid) b;  

单独统计pv,uv,IP,login等指标,并union拼起来,任务跑了不到1个小时就去来结果了


3、参数的优化

[sql]  view plain  copy
  1. SET mapred.reduce.tasks=50;  
  2. SET mapreduce.reduce.memory.mb=6000;  
  3. SET mapreduce.reduce.shuffle.memory.limit.percent=0.06;  


涉及数据倾斜的话,主要是reduce中数据倾斜的问题,可能通过设置Hive中reduce的并行数,reduce的内存大小单位为m,reduce中 shuffle的刷磁盘的比例,来解决。


原文地址:https://blog.csdn.net/u010159842/article/details/75351847

猜你喜欢

转载自blog.csdn.net/sunwukong_hadoop/article/details/80696392