使用 PIG 拉丁语计算百分比 [英] Calculating percentage using PIG latin

查看:21
本文介绍了使用 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.我假设这些列之间由单个 while 空格分隔,如果它是一个选项卡,然后在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆