猪脚本循环计算平均值
pig script loop though calculate averages
我有使用 aws emr 的猪 运行 看起来像的数据。这些列称为型号、年份、units_sold、total_customers.
chevy 1900 1000 49
chevy 1901 73 92
chevy 1902 45 65
chevy 1903 300 75
ford 1900 35 12
ford 1901 777 32
ford 1902 932 484
ford 1903 33 15
我想做的是计算每种车型的平均值。平均值的计算方法是将 units_sold 的总和除以 total_customers 的总和。
所以期望的结果看起来像
chevy (1000+73+45+300) / (49+92+65+75) = 5.04
ford (35+777+932+33) / (12+32+484+15) = 3.27
在我的脚本中我有
A = *Step to load data*;
B = GROUP A by year;
C = results = FOREACH B GENERATE SUM(units_sold)/SUM(total_customers);
dump C;
这个returns不正确result.How我能得到看起来像
的结果吗
chevy 5.04
ford 3.27
看来您需要按车型而不是年份分组。此外,如果 units_sold
和 total_customers
是整数,如果您不想要四舍五入的结果,则可能需要强制转换为浮点数。尝试:
B = GROUP A by model;
C = FOREACH B GENERATE (float)SUM(units_sold)/(float)SUM(total_customers);
我有使用 aws emr 的猪 运行 看起来像的数据。这些列称为型号、年份、units_sold、total_customers.
chevy 1900 1000 49
chevy 1901 73 92
chevy 1902 45 65
chevy 1903 300 75
ford 1900 35 12
ford 1901 777 32
ford 1902 932 484
ford 1903 33 15
我想做的是计算每种车型的平均值。平均值的计算方法是将 units_sold 的总和除以 total_customers 的总和。 所以期望的结果看起来像
chevy (1000+73+45+300) / (49+92+65+75) = 5.04
ford (35+777+932+33) / (12+32+484+15) = 3.27
在我的脚本中我有
A = *Step to load data*;
B = GROUP A by year;
C = results = FOREACH B GENERATE SUM(units_sold)/SUM(total_customers);
dump C;
这个returns不正确result.How我能得到看起来像
的结果吗chevy 5.04
ford 3.27
看来您需要按车型而不是年份分组。此外,如果 units_sold
和 total_customers
是整数,如果您不想要四舍五入的结果,则可能需要强制转换为浮点数。尝试:
B = GROUP A by model;
C = FOREACH B GENERATE (float)SUM(units_sold)/(float)SUM(total_customers);