Sqoop: Importing Table into Target Directory
Description
This Example describes how to import data from MySQL database to Hadoop HDFS.
Prerequisites
- Linux
- JDK 11
- Hadoop 3.4.1
- Sqoop 1.4.7
- MySQL
MySQL
- Download MySQL driver into
/home/hadoop/sqoop-1.4.7.bin__hadoop-2.6.0/lib
- Create database
sqoop_test
- Create table
emp
insqoop_test
, e.g.:
+----------+-------+
| PersonID | Name |
+----------+-------+
| 1 | Alice |
| 2 | Bob |
+----------+-------+
Import
sqoop import \
--connect jdbc:mysql://localhost/sqoop_test \
--username root -P \
--table emp --m 1 \
--target-dir /examples/emp1
Output:
...
...
Input split bytes=87
Spilled Records=0
Failed Shuffles=0
Merged Map outputs=0
GC time elapsed (ms)=7
CPU time spent (ms)=830
Physical memory (bytes) snapshot=281747456
Virtual memory (bytes) snapshot=2787991552
Total committed heap usage (bytes)=524288000
Peak Map Physical memory (bytes)=281747456
Peak Map Virtual memory (bytes)=2787991552
File Input Format Counters
Bytes Read=0
File Output Format Counters
Bytes Written=14
hdfs dfs -ls /examples/emp1
Output:
-rw-r--r-- 1 hadoop supergroup 0 2025-03-16 12:30 /examples/emp1/_SUCCESS
-rw-r--r-- 1 hadoop supergroup 14 2025-03-16 12:30 /examples/emp1/part-m-00000
hadoop fs -cat /examples/emp1/part-m-00000
Output
1,Alice
2,Bob