我有以下数据集:
Movies : moviename, genre1, genre2, genre3 ..... genre19
(以上所有类型的值均为0或1,1表示电影属于该类型)
现在我想找出哪部电影的类型最少?
我试过下面的Pig剧本:
items = load 'path' using PigStorage('|') as (mName:chararray,g1:int,g2:int,g3:int,g4:int,g5:int,g6:int,g7:int,g8:int,g9:int,g10:int,g11:int,g12:int,g13:int,g14:int,g15:int,g16:int,g17:int,g18:int,g19:int);
sumGenre = foreach items generate mName, g1+g2+g3+g4+g5+g6+g7+g8+g9+g10+g11+g12+g13+g14+g15+g16+g17+g18+g19 as sumOfGenres;
groupAll = group sumGenre All;
在下一步中,通过使用min(sumgree.sumofgenres),我可以得到一个类型,它是min值,但是我想要的是得到一个类型数最少的moviename,以及该电影的类型数。
有人能帮忙吗?
1我想知道有没有其他简单的方法得到g1+g2+…g19的和?
2还有输出:类型最少的电影?
2条答案
按热度按时间7hiiyaii1#
之后
groupAll
```r1 = minGenre = foreach groupAll generate MIN(sumGenre.sumOfGenres) as minG;
public class DynRowSum extends EvalFunc
{
public Integer exec(Tuple v) throws IOException
{
List olist = v.getAll();
int sum = 0;
int cnt=0;
for( Object o : olist){
cnt++;
if (cnt!=1) {
int val= (Integer)o;
sum = sum + val;
}
}
return new Integer(sum);
}
}
grunt>sumGenre = foreach items generate mName,DynRowSum(*) as sumOfGenres;
qpgpyjmq2#