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

来源:互联网 发布:linux查看端口nc 编辑:程序博客网 时间:2024/04/30 18:30

相关文章推荐:

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

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

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


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


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

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 实现去重

select "2015","PV",count(*) from dms.tracklog_5minwhere day>='2015' and day<'2016'union all select "201505","UV",count(*) from (select  cookieid from dms.tracklog_5minwhere day>='2015' and day<'2016'  group by cookieid ) a union all select "2015","IP",count(*) from (select  ip from dms.tracklog_5minwhere day>='2015' and day<'2016'  group by ip ) a union all select "2015","LOGIN",count(*) from (select  userid from dms.tracklog_5minwhere day>='2015' and day<'2016' group by userid) b;

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


3、参数的优化

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的刷磁盘的比例,来解决。



2 0
原创粉丝点击