使用PIG拉丁文计算百分比 [英] Calculating percentage using PIG latin
本文介绍了使用PIG拉丁文计算百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含两列的表(代码:chararray,sp:double)
I have a table with two columns (code:chararray, sp:double)
我想计算每个sp的百分比.
I want to calculate the percentage of every sp.
INPUT
t001 60
a002 75
a003 34
bb04 56
bbc5 23
cc2c 45
ddc5 45
所需的输出:
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?
推荐答案
您正在将第二列加载到名为sp的字段中,但将其称为speed.我认为这些列之间用一个空格隔开,如果它是一个选项卡,然后在LOAD语句中使用PigStorage('\ t').
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;
输出:
这篇关于使用PIG拉丁文计算百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文