TaylerJones
TaylerJones

Reputation: 242

Apache PIG - Join followed by projection results in NULLs

The below code works as expected:

a = load 'data_a' using PigStorage('\t') as (a1, a2, a3);
b = load 'data_b' using PigStorage('\t') as (b1, b2, b3;
a_b = join a by a1, b by b1; --inner join

When I inspect the fields, they are populated correctly.

However, once I add a projection into the mix, it doesn't work.

a = load 'data_a' using PigStorage('\t') as (a1, a2, a3);
b = load 'data_b' using PigStorage('\t') as (b1, b2, b3;
a_b = join a by a1, b by b1; --inner join
ab = foreach a_b generate a1 as a1, a2 as a2, b2 as b2;

In ab, all cells in the fields from b are NULL.

The same thing happens if I do this:

a = load 'data_a' using PigStorage('\t') as (a1, a2, a3);
a2 = foreach a generate a1, a2;
b = load 'data_b' using PigStorage('\t') as (b1, b2, b3;
b2 = foreach b generate b1, b2;
ab = join a2 by a1, b2 by b1;

I use the following workaround, but hate being bogged down by the store/load:

a = load 'data_a' using PigStorage('\t') as (a1, a2, a3);
b = load 'data_b' using PigStorage('\t') as (b1, b2, b3;
a_b = join a by a1, b by b1; --inner join
store a_b into 'hdfs:///a_b_temp' using PigStorage('\t','-schema');
a_b2 = load 'hdfs:///a_b_temp' using PigStorage('\t');
ab = foreach a_b2 generate a1 as a1, a2 as a2, b2 as b2;

And the fields in ab do not become NULL. However, if I then group and perform aggregations, I typically get the error:

ERROR org.apache.pig.tools.pigstats.SimplePigStats - ERROR: org.apache.pig.data.DataByteArray cannot be cast to java.lang.Long

However, this error goes away if I skip the last projection.

I am new to Pig - are there any known bugs/issues that could be causing this? I have observed it happening several times with different data sets.

I am using pig 0.12 on Amazon AWS EMR.

Thanks for any help!

Upvotes: 0

Views: 88

Answers (1)

Sandeep Singh
Sandeep Singh

Reputation: 7990

I tried with your second approach and here is the code.

a = load '/user/root/pig/file1.txt' using PigStorage('\t') as (a1:int, a2:chararray, a3:chararray);
b = load '/user/root/pig/file2.txt' using PigStorage('\t') as (b1:int, b2:chararray, b3:chararray);

--inner join
a_b = join a by a1, b by b1; 

--if your goal is to get selected field from relation b based on join condition.
--a::a1 says "there is a record from "a" and that has a column called a1"
ab = foreach a_b generate a::a1, a2, b2;

--If your goal is to get all matching data on id from both relations.
--ab = foreach a_b generate $0..;

DUMP ab;

Hope it will help you.

Upvotes: 1

Related Questions