Pig, how to refer to a field after a join and a group by
Asked Answered
H

1

10

I have this code in Pig (win, request and response are just tables loaded directly from filesystem):

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);

Basically I want to sum the bid_price after joining and grouping, but I get an error:

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

My guess is that I'm not referring correctly to win.bid_price.

Hoi answered 30/10, 2012 at 18:52 Comment(0)
M
7

When performing multiple joins I recommend using unique identifiers for your fields (e.g. for bid_id). Alternatively, you can also use the disambiguation operator '::', but that can get pretty dirty.

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);
Meaty answered 31/10, 2012 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.