猪,如何在一个连接和一个组之后引用一个字段

时间:2012-10-30 18:52:49

标签: hadoop apache-pig

我在Pig中有这个代码(win,request和response只是直接从文件系统加载的表):

win_request = JOIN win BY bid_id, request BY bid_id;
win_request_response = JOIN win_request BY win.bid_id, response BY bid_id;

win_group = GROUP win_request_response BY (win.campaign_id);

win_count = FOREACH win_group GENERATE group, SUM(win.bid_price);

基本上我想在加入和分组后总结bid_price,但是我收到错误:

Could not infer the matching function for org.apache.pig.builtin.SUM as multiple or none of them fit. Please use an explicit cast.

我的猜测是我没有正确引用win.bid_price

1 个答案:

答案 0 :(得分:7)

执行多个联接时,我建议您为字段使用唯一标识符(例如,对于bid_id)。或者,您也可以使用disambiguation operator'::',但这可能会非常脏。

wins = LOAD '/user/hadoop/rtb/wins' USING PigStorage(',') AS (f1_w:int, f2_w:int,  f3_w:chararray);
reqs = LOAD '/user/hadoop/rtb/reqs' USING PigStorage(',') AS (f1_r:int, f2_r:int, f3_r:chararray);
resps = LOAD '/user/hadoop/rtb/resps' USING PigStorage(',') AS (f1_rp:int, f2_rp:int, f3_rp:chararray);

wins_reqs = JOIN wins BY f1_w, reqs BY f1_r;
wins_reqs_reps = JOIN wins_reqs BY f1_r, resps BY f1_rp;

win_group = GROUP wins_reqs_reps BY (f3_w);

win_sum = FOREACH win_group GENERATE group, SUM(wins_reqs_reps.f2_w);