我有一个文件如下
姓名得分
约翰aa
约翰aa
约翰aa
约翰bb
mary cc
mary cc
玛丽dd
我想按每个人的分数输出他们的分数百分比 所以它看起来像这样
约翰aa 75John有3 aa和1 bb,所以aa%= 75,bb%= 25 我想在Hadoop猪中做,请帮助,谢谢
-Troy
答案 0 :(得分:1)
你能试试吗?
输入:file.dat
john aa
john aa
john aa
john bb
mary cc
mary cc
mary dd
代码:
A = LOAD 'file.dat' USING PigStorage(' ') as (name:chararray,score:chararray);
N = CUBE A BY CUBE(name,score);
N2 = FOREACH N GENERATE FLATTEN(group) AS (name,score), ((float)COUNT_STAR(cube)) As (totcnt:float);
N3 = FILTER N2 BY name!='null';
N4 = GROUP N3 BY name;
N5 = FOREACH N4 {
fil = order N3 BY score;
fil1 = LIMIT fil 1;
fil2 = FILTER N3 BY score!='null';
generate FLATTEN(fil2) AS (name:chararray,score:chararray,indcount:float),FLATTEN(fil1.totcnt) as (totcnt:float);
}
N6 = FOREACH N5 GENERATE name,score,(indcount/totcnt)*100;
DUMP N6;
输出:
(john,aa,75.0)
(john,bb,25.0)
(mary,cc,66.66667)
(mary,dd,33.333336)