pig学习小记1

前提:假如有一批输入数据格式如下:

用户id|文章类别|浏览次数

 

 

 

 

 

 

要求:用pig求出每个用户浏览次数最多的文章类别?

1.首先:读入数据

– 指定分隔符为 “|”
– 指定每个字段的类型

进入pig,  u_ct=load'/output' using PigStorage('|') as(user:chararray,category:chararray,times:int);

2.分用户统计:
– group:对相同user的记彔进行聚合
– sorted: 根据times从大到小进行排序
– top: 保留sorted的第一行

u_stat=foreach (group u_ct by user){sorted= order u_ct by times desc; top= limit sorted 1; generate flatten(top),SUM(u_ct.times)};

3.输出: store u_stat into '/stat';

4 展示:cat /stat

posted @ 2015-04-23 19:11  孟想阳光  阅读(88)  评论(0)    收藏  举报