如何在过滤掉不同键的同时对 Apache Pig 中的值列进行计数

问题描述 投票:0回答:1

我正在尝试通过

product_id
来计算订购产品的总数(数量)。

数据看起来像这样。

(产品 ID,数量)

这也是

(11, 5)
(11, 2)
(11, 1)
(12, 9)
(12, 1)
(13, 5)
(13, 9)
(13, 9)

会出现重复的product_id,因为已订购多次且每次的数量不同。

如何计算每个product_id的总数量?

预计出来的将会是

(11, 8)
(12, 10)
(13, 23)
apache-pig
1个回答
0
投票

您可以使用GROUP BY

假设您在名为 products 的变量中拥有预期的输入数据。

count_prod = FOREACH (GROUP products BY product_id) {
get_one_record = LIMIT product 1;
GENERATE FLATTEN(get_one_record), SUM(quantity) AS total_quantity;
}; 
final_products_data = FOREACH count_prod GENERATE get_one_record::product_id AS product_id, total_quantity;

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.