📖 Scenario: You work as a data analyst in a company that stores customer data in a MySQL database. You want to analyze this data using Hadoop tools. To do this, you need to import the customer data from the MySQL database into Hadoop's HDFS using Sqoop.
🎯 Goal: Learn how to use Sqoop commands to import data from a MySQL database table into HDFS step-by-step.
📋 What You'll Learn
Use Sqoop to connect to a MySQL database
Import a specific table called customers from the database
Specify the target directory in HDFS for the imported data
Use a condition to import only customers from a specific country
Display the imported data files in HDFS
💡 Why This Matters
🌍 Real World
Companies often store data in relational databases but want to analyze it using big data tools like Hadoop. Sqoop helps move data easily from databases to Hadoop.
💼 Career
Data engineers and analysts use Sqoop to import data for analysis, reporting, and building data pipelines.
Progress0 / 4 steps
1
Set up the database connection details
Create variables for the database connection details: db_url with value jdbc:mysql://localhost:3306/companydb, db_user with value root, and db_password with value password123.
Hadoop
Hint
Use three variables named exactly db_url, db_user, and db_password with the given string values.
2
Set the target HDFS directory and table name
Create variables table_name with value customers and target_dir with value /user/hadoop/customers_data.
Hadoop
Hint
Define table_name and target_dir variables with the exact values.
3
Write the Sqoop import command with a condition
Create a variable sqoop_command that stores the full Sqoop import command as a string. The command should use db_url, db_user, db_password, table_name, and target_dir. Add a condition to import only customers where country = 'USA'. Use the format: sqoop import --connect {db_url} --username {db_user} --password {db_password} --table {table_name} --where "country='USA'" --target-dir {target_dir} --delete-target-dir.
Hadoop
Hint
Use an f-string to build the Sqoop command with the exact options and condition.
4
Print the Sqoop command to run
Write a print statement to display the sqoop_command variable.
Hadoop
Hint
Use print(sqoop_command) to show the full command.
Practice
(1/5)
1. What is the main purpose of Sqoop in Hadoop?
easy
A. To process data inside Hadoop using MapReduce
B. To export data from Hadoop to relational databases
C. To import data from relational databases into Hadoop
D. To visualize data stored in Hadoop
Solution
Step 1: Understand Sqoop's role
Sqoop is designed to transfer data between relational databases and Hadoop.
Step 2: Identify import function
Its main use is importing data from databases into Hadoop's storage.
Final Answer:
To import data from relational databases into Hadoop -> Option C
Quick Check:
Sqoop imports data = A [OK]
Hint: Remember Sqoop imports data into Hadoop from databases [OK]
Common Mistakes:
Confusing import with export
Thinking Sqoop processes data inside Hadoop
Assuming Sqoop is for visualization
2. Which of the following is the correct basic Sqoop import command syntax?
easy
A. sqoop export --connect jdbc:mysql://localhost/db --table mytable --target-dir /user/hadoop/mytable
B. sqoop import --connect jdbc:mysql://localhost/db --table mytable --target-dir /user/hadoop/mytable
C. sqoop import --table mytable --connect jdbc:mysql://localhost/db --output-dir /user/hadoop/mytable
D. sqoop import --connect jdbc:mysql://localhost/db --table mytable --output-dir /user/hadoop/mytable
Solution
Step 1: Identify correct command type
Import commands use 'sqoop import' with connection, table, and target directory.
The command uses --num-mappers 4 but does not specify --split-by column.
Step 2: Understand parallel import requirements
Sqoop requires a --split-by column to divide data for multiple mappers; missing it causes error.
Final Answer:
Missing --split-by column for parallel import -> Option A
Quick Check:
Parallel import needs --split-by column [OK]
Hint: Always specify --split-by when using multiple mappers [OK]
Common Mistakes:
Assuming --num-mappers works without --split-by
Thinking --target-dir format causes error
Ignoring table existence check
5. You want to import a large 'orders' table from MySQL into Hadoop quickly. The table has a numeric primary key 'order_id'. Which Sqoop command will best speed up the import?