Sqoop helps move data from databases into Hadoop easily. It saves time and avoids manual copying.
Sqoop for database imports in Hadoop
sqoop import \ --connect jdbc:mysql://hostname:port/dbname \ --username user \ --password pass \ --table tablename \ --target-dir /hdfs/path \ [other options]
Use --connect to specify the database connection string.
--table tells Sqoop which table to import.
customers table from MySQL database salesdb into HDFS directory /user/hadoop/customers.sqoop import --connect jdbc:mysql://localhost:3306/salesdb --username admin --password secret --table customers --target-dir /user/hadoop/customers
transactions table from PostgreSQL, splitting import by transaction_id for parallelism.sqoop import --connect jdbc:postgresql://dbserver:5432/finance --username finance_user --password pass123 --table transactions --split-by transaction_id --target-dir /data/transactions
$CONDITIONS is required for splitting.sqoop import --connect jdbc:mysql://localhost:3306/hrdb --username hr --password hrpass --query 'SELECT * FROM employees WHERE department = "Sales" AND $CONDITIONS' --split-by employee_id --target-dir /user/hadoop/sales_employees
This command imports the employees table from the MySQL database testdb into the HDFS directory /user/hadoop/employees. It uses 2 mappers to speed up the import.
sqoop import \ --connect jdbc:mysql://localhost:3306/testdb \ --username testuser \ --password testpass \ --table employees \ --target-dir /user/hadoop/employees \ --num-mappers 2
Always use $CONDITIONS in custom queries to allow Sqoop to split the data for parallel import.
Use --num-mappers to control parallelism; more mappers can speed up import but use more resources.
Passwords can be passed securely using --password-file instead of plain text.
Sqoop imports data from databases into Hadoop automatically.
Use connection string, table name, and target directory to specify import.
Parallel imports speed up data transfer using split columns and mappers.
Practice
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 CQuick Check:
Sqoop imports data = A [OK]
- Confusing import with export
- Thinking Sqoop processes data inside Hadoop
- Assuming Sqoop is for visualization
Solution
Step 1: Identify correct command type
Import commands use 'sqoop import' with connection, table, and target directory.Step 2: Check option correctness
sqoop import --connect jdbc:mysql://localhost/db --table mytable --target-dir /user/hadoop/mytable uses correct flags: --connect, --table, --target-dir.Final Answer:
sqoop import --connect jdbc:mysql://localhost/db --table mytable --target-dir /user/hadoop/mytable -> Option BQuick Check:
Correct import syntax = A [OK]
- Using 'export' instead of 'import'
- Mixing --output-dir with import command
- Incorrect order of parameters
sqoop import --connect jdbc:mysql://localhost/db --table employees --split-by emp_id --num-mappers 4 --target-dir /user/hadoop/employees
Solution
Step 1: Analyze command options
The command imports data from 'employees' table using 4 mappers and splits data by 'emp_id'.Step 2: Understand parallel import
Using --split-by with --num-mappers enables parallel import for faster data transfer.Final Answer:
Imports employees table using 4 parallel mappers splitting by emp_id -> Option DQuick Check:
--split-by + --num-mappers = parallel import [OK]
- Thinking --split-by disables parallelism
- Confusing import with export
- Assuming default 1 mapper always used
sqoop import --connect jdbc:mysql://localhost/db --table sales --num-mappers 4 --target-dir /user/hadoop/sales
What is the most likely cause?
Solution
Step 1: Check command parameters
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 AQuick Check:
Parallel import needs --split-by column [OK]
- Assuming --num-mappers works without --split-by
- Thinking --target-dir format causes error
- Ignoring table existence check
Solution
Step 1: Identify best split column
Use the numeric primary key 'order_id' for --split-by to evenly divide data.Step 2: Use multiple mappers for parallel import
Setting --num-mappers 8 enables 8 parallel tasks to speed up import.Step 3: Evaluate other options
sqoop import --connect jdbc:mysql://localhost/db --table orders --target-dir /user/hadoop/orders uses single mapper (slow). sqoop import --connect jdbc:mysql://localhost/db --table orders --num-mappers 8 --target-dir /user/hadoop/orders misses --split-by (error). sqoop import --connect jdbc:mysql://localhost/db --table orders --split-by order_date --num-mappers 8 --target-dir /user/hadoop/orders splits by 'order_date' which may not be numeric or evenly distributed.Final Answer:
sqoop import --connect jdbc:mysql://localhost/db --table orders --split-by order_id --num-mappers 8 --target-dir /user/hadoop/orders -> Option AQuick Check:
Use numeric PK with --split-by and multiple mappers [OK]
- Not specifying --split-by with multiple mappers
- Choosing non-numeric or uneven split column
- Using single mapper for large tables
Ready to prove it?
Pass the test and this pattern moves unknown
