Created
August 8, 2013 21:55
-
-
Save matthayes/6189135 to your computer and use it in GitHub Desktop.
When joining on more than two relations, a better option than using multiple left joins is to use a cogroup. However the code gets pretty ugly. This code uses the insight that the input1 bag will be empty when there is no match, and flattening this removes the record. If the input2 or input3 bags are empty we don't want flattening them to remove…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input1 = LOAD 'input1' using PigStorage(',') AS (val1:INT,val2:INT); | |
input2 = LOAD 'input2' using PigStorage(',') AS (val1:INT,val2:INT); | |
input3 = LOAD 'input3' using PigStorage(',') AS (val1:INT,val2:INT); | |
data1 = COGROUP input1 BY val1, input2 BY val1, input3 BY val1; | |
data2 = FOREACH data1 GENERATE | |
FLATTEN(input1), -- left join on this | |
FLATTEN((IsEmpty(input2) ? TOBAG(TOTUPLE((int)null,(int)null)) : input2)) | |
as (input2::val1,input2::val2), | |
FLATTEN((IsEmpty(input3) ? TOBAG(TOTUPLE((int)null,(int)null)) : input3)) | |
as (input3::val1,input3::val2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment