In SQOOP import command is used to import RDBMS data into HDFS. Using import command we can import a particular table into HDFS.
In this example I will show how we can import MySQL database table into HDFS.
First of all create database using following command.
To list all databases use the following command
Now create the table employee in following manner
Now using following command you can see the table
Now add some records in this emp table
You can see the following records
Now you can import this emp table into hdfs by using following sqoop import command
Sqoop import - - connect jdbc:mysql://localhost:3306/databasename - - username u_name - -password pwd - - table table_name
Note: - while executing this command if you will get any access denied issues then first execute the following grant command
If command will run successfully then you will get final following output.
Note: - I am showing the last screen shot of the output window as its very large image to display
Now if you want to see these files stored in HDFS then execute the following command
Note: - if we don’t specify the output directory then sqoop creates the directory with the name of table which you have uploaded in HDFS and divide the data in part-m-00000, part-m-00001 so on.
As you can see the figure its showing it launched 3 map task so three output file will be get generated and i.e part-m-00000, part-m-00001 and part-m-00002.
If we want to specify the count of map task then we can use - - m option with numeric number.
As in the previous example has seen the example in which we didn’t assign the target directory. If we want then using target-dir option we can specify the directory.
In this way you can check the arguments of different commands of SQOOP
When we execute this command you will see only one map task will be get executed.
And if you will list the directory by using following command you will find only one output file i.e Part-m-00000
In our previous example we see how we can import complete table in HDFS. SQOOP import with where clause allows us to import specific records in HDFS.
For example I want to import only those records who are having age equal and greater than 26. In our emp table we have 2 records having age greater than equal to 26.
So we will write sqoop import command using where clause in the following manner:-
For any query you can send mail at firstname.lastname@example.org Thanks