1

我有一个文件:

1,Mary,5
1,Tom,5
2,Bill,5
2,Sue,4
2,Theo,5
3,Mary,5
3,Cindy,5
4,Andrew,4
4,Katie,4
4,Scott,5
5,Jeff,3
5,Sara,4
5,Ryan,5
6,Bob,5
6,Autumn,4
7,Betty,5
7,Janet,5
7,Scott,5
8,Andrew,4
8,Katie,4
8,Scott,5
9,Mary,5
9,Tom,5
10,Bill,5
10,Sue,4
10,Theo,5
11,Mary,5
11,Cindy,5
12,Andrew,4
12,Katie,4
12,Scott,5
13,Jeff,3
13,Sara,4
13,Ryan,5
14,Bob,5
14,Autumn,4
15,Betty,5
15,Janet,5
15,Scott,5
16,Andrew,4
16,Katie,4
16,Scott,5 

我想要名字出现最多的答案,即 max (Scott,6)

4

1 回答 1

1

你的问题有些模棱两可。

你到底想要什么。

您想要按降序排列的用户数列表吗?

或者

您只想要 (scott,6) 即只有一个最大数量的用户吗?

在您提供的示例数据上,我已经成功解决了这两个问题。

如果问题是第一种类型,那么,

a = load '/file.txt' using PigStorage(',') as (id:int,name:chararray,number:int);
g = group a by name;
g1 = foreach g{ 
      generate group as g , COUNT(a) as cnt;
}; 
toptemp  = group g1 all; 
final = foreach toptemp{
        sorted = order g1 by cnt desc;
        GENERATE flatten(sorted);
};

这将为您提供按降序排列的用户列表,

(Scott,6)
(Katie,4)
(Andrew,4)
(Mary,4)
(Bob,2)
(Sue,2)
(Tom,2)
(Bill,2)
(Jeff,2)
(Ryan,2)
(Sara,2)
(Theo,2)
(Betty,2)
(Cindy,2)
(Janet,2)
(Autumn,2)

如果问题属于第二种类型,那么,

a = load '/file.txt' using PigStorage(',') as (id:int,name:chararray,number:int);
g = group a by name;
g1 = foreach g{ 
      generate group as g , COUNT(a) as cnt;
}; 
toptemp  = group g1 all; 
final = foreach toptemp{
        sorted = order g1 by cnt desc;
        top = limit sorted 1;     
        GENERATE flatten(top);
};

这只给了我们一个结果,

(Scott,6)

谢谢。我希望它有所帮助。

于 2014-01-20T07:10:10.967 回答