In Pig, a join operation combines records from two or more input relations based on a common field or fields. There are different types of join operations available in Pig, such as inner join, outer join, and skewed join. Let’s discuss these join types and how to perform them in Pig.
1. Inner Join:
Inner join returns only those records from the input relations that have matching values in the specified join keys. The syntax for performing an inner join in Pig is as follows:
joined_data = JOIN relation1 BY key1, relation2 BY key2 [, relationN BY keyN] [USING 'replicated'|'skewed'|'merge'|'repartition'];
Here’s an example:
Suppose we have two relations student and courses:
student:
(1, John)
(2, Mary)
(3, Peter)
courses:
(1, Math)
(1, Science)
(2, English)
To perform an inner join on these relations, we can write the following script:
students = LOAD 'student.txt' USING PigStorage() AS (id: int, name: chararray);
courses = LOAD 'courses.txt' USING PigStorage() AS (student_id: int, subject: chararray);
joined_data = JOIN students BY id, courses BY student_id;
This join operation will produce the following output:
(1, John, 1, Math)
(1, John, 1, Science)
(2, Mary, 2, English)
2. Outer Join:
Outer join returns records from one or more input relations even if there’s no match in the other input relations. Pig supports left, right, and full outer joins. The syntax for performing an outer join in Pig is as follows:
joined_data = JOIN relation1 BY key1 [LEFT|RIGHT|FULL] OUTER, relation2 BY key2 [, relationN BY keyN] [USING 'replicated'|'skewed'|'merge'|'repartition'];
Here’s an example using the left outer join:
students = LOAD 'student.txt' USING PigStorage() AS (id: int, name: chararray);
courses = LOAD 'courses.txt' USING PigStorage() AS (student_id: int, subject: chararray);
joined_data = JOIN students BY id LEFT OUTER, courses BY student_id;
This join operation will produce the following output:
(1, John, 1, Math)
(1, John, 1, Science)
(2, Mary, 2, English)
(3, Peter, NULL, NULL)
3. Skewed Join:
A skewed join is an optimization for joins in Pig when the join key distribution is skewed. It means one key has a large number of records associated with it compared to other keys. The syntax is the same as the inner and outer join operations; however, you need to add the ’skewed’ keyword to the USING clause.
joined_data = JOIN relation1 BY key1, relation2 BY key2 USING 'skewed';
In summary, Pig provides various types of join operations to combine records from multiple relations. You can use inner, outer, and skewed joins based on your use case, and join keys to perform join operations in Pig.