Calculating percentage using PIG latin

扶醉桌前 提交于 2019-12-12 02:36:49

问题


I have a table with two columns (code:chararray, sp:double)

I want to calculate the percentage of every sp.

INPUT
t001 60
a002 75
a003 34
bb04 56
bbc5 23
cc2c 45
ddc5 45

desired OUTPUT:

code Perc
t001 17%
a002 22%
a003 10%
bb04 16.5%
bbc5 6%
cc2c 13.3%
ddc5 13.3%

I tried like this but output is not coming.

A = load '....' as (code : chararray, sp : double); 
B = GROUP A BY (code); 
allcount = FOREACH B GENERATE SUM(A.speed) as total; 
perc = FOREACH A GENERATE code,speed/(double)allcount.total * 100; 
dump perc;

How can i do using pig latin?


回答1:


You are loading the second column into a field called sp but referring to it as speed.I presume that the columns are separated by a single while space,if it is a tab then use PigStorage('\t') in the LOAD statement.

A = LOAD '/YourFilePath/YourFile.txt' USING PigStorage(' ') AS (code:chararray, sp:double); 
B = GROUP A ALL; 
C = FOREACH B GENERATE SUM(A.sp) AS total; 
D = FOREACH A GENERATE code,ROUND_TO((sp/(double)C.total) * 100,2) AS perc;
E = FOREACH D GENERATE code,CONCAT((chararray)perc,'%'); 
DUMP E;

OUTPUT:



来源:https://stackoverflow.com/questions/38383412/calculating-percentage-using-pig-latin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!