Skip to content

Instantly share code, notes, and snippets.

@matthayes
Last active December 20, 2015 20:18
Show Gist options
  • Save matthayes/6189181 to your computer and use it in GitHub Desktop.
Save matthayes/6189181 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. Here we use the EmptyBagToNullFields from DataFu to make the code very concise. 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 …
DEFINE EmptyBagToNullFields datafu.pig.bags.EmptyBagToNullFields();
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),
FLATTEN(EmptyBagToNullFields(input2)),
FLATTEN(EmptyBagToNullFields(input3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment