hive使用技巧_巧用groupby实现去重统计

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

 

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

 

  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. ;  
select substr(day,1,4) year,count(*) PV,count(distinct cookieid) UV,count(distinct ip) IP,count(distinct userid) LOGIN 
from dms.tracklog_5min a  
where substr(day,1,4)='2015'
group by substr(day,1,4)
;

 

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

 

2、利用group by 实现去重

 

  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;  
select "2015","PV",count(*) from dms.tracklog_5min
where day>='2015' and day<'2016'
union all 
select "201505","UV",count(*) from (
select  cookieid from dms.tracklog_5min
where day>='2015' and day<'2016'  group by cookieid ) a 
union all 
select "2015","IP",count(*) from (
select  ip from dms.tracklog_5min
where day>='2015' and day<'2016'  group by ip ) a 
union all 
select "2015","LOGIN",count(*) from (
select  userid from dms.tracklog_5min
where day>='2015' and day<'2016' group by userid) b;


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

 

 

3、参数的优化

 

  1. SET mapred.reduce.tasks=50;  
  2. SET mapreduce.reduce.memory.mb=6000;  
  3. SET mapreduce.reduce.shuffle.memory.limit.percent=0.06;  
SET mapred.reduce.tasks=50;
SET mapreduce.reduce.memory.mb=6000;
SET mapreduce.reduce.shuffle.memory.limit.percent=0.06;

 

 

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

posted @ 2017-10-16 10:57  FanLei_Data  阅读(1694)  评论(0)    收藏  举报