Updated July 6, 2023
Introduction to Joins in Hive
Joins are used to retrieve various outputs using multiple tables by combining them based on particular columns. Now, for the tables to be in Hive, we are required to create the tables and load the data in each table. We are going to use two tables (customer and product) here for understanding the purpose.
Different Commands
Below are the commands for creating and loading the data in these tables:
For Customer Table: 6 rows
Create Command
Create an external table if not exists customer(id string, name string, city string)
row format delimited
fields terminated by ‘ ‘
location ‘/user/hive/warehouse/test.db/customer’
tblproperties (“skip.header.line.count”=”1”);
Load Command
Load data local in path ‘/home/cloudera/Customer_Neha.txt’ into table customer;
Customer Table Data
For Product table: 6 rows
Create Command
Create an external table if not exists product(Cust_Id string,
Product string, Price string)
row format delimited
fields terminated by ‘ ‘
location ‘/user/hive/warehouse/test.db/product’
tblproperties (“skip.header.line.count”=”1”);
Load command
Load data local path ‘/home/cloudera/Product_Neha.txt’ into table product;
Product Table Data
To check table schema, use the command “desc table name;”
Now, we have data in tables, Let’s play with it 😉
Types of Joins in Hive
Join- This will give the cross product of both the table’s data as output. As you can see, we have 6 rows in each table. So the output for Join will be 36 rows. The number of mappers-1. However, there no reduce to the operator is used.
Command
Output:
1. Full Join
Full Join without match condition will give the cross product of both tables.
Number of mappers-2
Number of reducer-1
This can be achieved using “Join” as well but with less number of mapper and reducer.
Full Join with match condition
All the rows will be joined from both tables. If rows are not matched in another table, then NULL will be populated in output (Observe Id-100,106). No rows are skipped.
Number of mappers-2
Number of reducer-1
Command
Output:
2. Inner Join
If the inner join is used without the “on” clause, it will give the cross product as the output. However, we are required to use the specific columns on which basis the join can be performed. The Id column from the customer table and Cust_id column from the product table are my specific columns. The output contains the rows where Id and Cust_Id matches. You can observe, Rows with Id-106 and Cust_Id-100 are skipped in output because they are not present in another table.
Command
Output:
3. Left Join
All the rows from the left table are joined with matched rows from the right table. If the right table has rows with ids that are not present in the left table, then those rows will be skipped (Observe Cust_Id-100 in output). If the right table does not have rows with ids that are there in the left table, NULL will be populated in output (Observe Id-106 in output).
Number of Mapper-1
Number of reducer-0
Command
Output:
4. Right Join
All the rows from the right table are matched with left table rows. If the left table does not have any row, then NULL will be populated (Observe Id 100). Rows from the left table will be skipped if that match is not found in the right table (Observe Id 106).
Number of Mapper-1
Number of reducer-0
Command
Output:
Conclusion
“Join” as the word suggests, can join two or more tables in the database. It is similar to joins in SQL. Joins are used to retrieve various outputs using multiple tables by combining them based on particular columns. Based on the requirement, it can be decided which join will work for you. For example, If you want to check what are the id’s present in the left table but not in the right table, you can simply use left join. Various optimizations can be performed in hive joins depending upon the complexity. Some of the examples are repartition joins, replication joins, and semi joins.
Recommended Articles
This is a guide to Joins in Hive. Here we discuss the basic concept, types of joins like full join, inner join, left join and right join in hive along with its command and output. You may also look at the following articles to learn more –