Desert Dragon Data Integration

HadoopHadoopBeginner
Practice Now

Introduction

In the vast expanse of a barren desert, a fierce sandstorm rages, whipping up clouds of sand that obscure the horizon. Amidst the swirling chaos, a majestic desert dragon soars through the sky, its scales glistening with a brilliant hue of gold. This mighty creature is tasked with a critical mission: to gather vital information from scattered data sources and uncover hidden insights that could aid in taming the relentless sandstorm.

The goal of this lab is to equip you with the skills to harness the power of Hadoop and Hive, enabling you to join and combine data from multiple sources. By mastering the art of joining data, you will unlock the ability to unravel complex relationships and uncover valuable patterns, just as the desert dragon seeks to unravel the secrets of the raging sandstorm.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL hadoop(("`Hadoop`")) -.-> hadoop/HadoopHiveGroup(["`Hadoop Hive`"]) hadoop/HadoopHiveGroup -.-> hadoop/join("`join Usage`") subgraph Lab Skills hadoop/join -.-> lab-288982{{"`Desert Dragon Data Integration`"}} end

Setting Up the Environment

In this step, you will prepare the necessary environment for working with Hadoop and Hive.

  1. Open a terminal window and switch to the hadoop user by running the following command:
su - hadoop
  1. Navigate to the /home/hadoop directory by running the following command:
cd /home/hadoop
  1. Create a new directory called join_lab to store your files:
mkdir join_lab
  1. Change to the join_lab directory:
cd join_lab

Now you have a dedicated workspace for this lab.

Creating Sample Datasets

In this step, you will create two sample datasets to practice joining operations in Hive.

  1. Create a new file named employees.txt with the following content:
101,John Doe,Sales
102,Jane Smith,Marketing
103,Michael Johnson,IT
104,Emily Davis,HR
105,Adam Wilson,Finance
106,Lisa Brown,Operations
  1. Create another file named departments.txt with the following content:
1,Sales,New York
2,Marketing,Los Angeles
3,IT,Chicago
4,HR,San Francisco
  1. Launch the Hive shell by executing the following command:
hive
  1. Load the datasets into Hive by running the following commands:
CREATE TABLE employees (emp_id INT, name STRING, dept STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

LOAD DATA LOCAL INPATH '/home/hadoop/join_lab/employees.txt' OVERWRITE INTO TABLE employees;

CREATE TABLE departments (dept_id INT, dept_name STRING, location STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

LOAD DATA LOCAL INPATH '/home/hadoop/join_lab/departments.txt' OVERWRITE INTO TABLE departments;

These commands will create two Hive tables, employees and departments, and load the data from the respective text files.

Performing Inner Join

In this step, you will learn how to perform an inner join operation in Hive to combine data from the employees and departments tables.

Execute the following Hive query to perform an inner join:

CREATE TABLE result_1
AS
SELECT e.emp_id, e.name, d.dept_name, d.location
FROM employees e
JOIN departments d
ON e.dept = d.dept_name;

SELECT * FROM result_1;

This query joins the employees and departments tables based on the dept and dept_name columns, respectively. It retrieves the emp_id, name, dept_name, and location columns from the joined result.

The output should look like this:

101     John Doe        Sales   New York
102     Jane Smith      Marketing       Los Angeles
103     Michael Johnson IT      Chicago
104     Emily Davis     HR      San Francisco

An inner join combines rows from two tables based on a specified condition. In this case, it matches the dept values from the employees table with the dept_name values from the departments table. The result includes only the rows where the join condition is satisfied, meaning the dept and dept_name values match.

Performing Left Outer Join

In this step, you will learn how to perform a left outer join operation in Hive.

Execute the following Hive query to perform a left outer join:

CREATE TABLE result_2
AS
SELECT e.emp_id, e.name, d.dept_name, d.location
FROM employees e
LEFT OUTER JOIN departments d
ON e.dept = d.dept_name;

SELECT * FROM result_2;

This query performs a left outer join between the employees and departments tables. It includes all rows from the left table (employees) and the matching rows from the right table (departments). If there is no match in the right table, the result will contain NULL values for the columns from the right table.

The output should look like this:

101     John Doe        Sales   New York
102     Jane Smith      Marketing       Los Angeles
103     Michael Johnson IT      Chicago
104     Emily Davis     HR      San Francisco
105     Adam Wilson     NULL    NULL
106     Lisa Brown      NULL    NULL

A left outer join returns all rows from the left table (employees) and the matching rows from the right table (departments). If a row in the left table has no matching row in the right table, the result will include NULL values for the columns from the right table.

Summary

In this lab, you learned how to work with Hadoop and Hive to perform join operations on datasets. You created sample datasets and loaded them into Hive tables, enabling you to practice inner and left outer joins. By mastering these join operations, you have gained the ability to combine and analyze data from multiple sources, just as the desert dragon combines information from scattered sources to unravel the secrets of the raging sandstorm.

Throughout this lab, I aimed to provide a structured and immersive learning experience, guiding you through each step with clear instructions, code examples, and explanations. The use of checkers ensured that you could validate your progress and identify areas for improvement. By designing a scenario that resonates with the challenges faced in data analysis, I hope to have made the learning process more engaging and relevant.

Overall, this lab has not only equipped you with technical skills in Hadoop and Hive but has also fostered a deeper understanding of the importance of data integration and analysis in solving complex problems. Just as the desert dragon perseveres in its mission, your newfound knowledge will empower you to tackle real-world data challenges with confidence and creativity.

Other Hadoop Tutorials you may like