I know how to use sqoop through command line. But dont know how to call sqoop command using java programs . Can anyone give some code view?
You can run sqoop from inside your java code by including the sqoop jar in your classpath and calling the Sqoop. runTool() method. You would have to create the required parameters to sqoop programmatically as if it were the command line (e.g. --connect etc.).
Importing a Table. Sqoop tool 'import' is used to import table data from the table to the Hadoop file system as a text file or a binary file. The following command is used to import the emp table from MySQL database server to HDFS. If it is executed successfully, then you get the following output.
You can run sqoop from inside your java code by including the sqoop jar in your classpath and calling the Sqoop.runTool()
method. You would have to create the required parameters to sqoop programmatically as if it were the command line (e.g. --connect
etc.).
Please pay attention to the following:
Sqoop.runTool()
as opposed to Sqoop.Main()
is the fact that runTool()
return the error code of the execution.Hope that helps.
final int ret = Sqoop.runTool(new String[] { ... }); if (ret != 0) { throw new RuntimeException("Sqoop failed - return code " + Integer.toString(ret)); }
RL
Find below a sample code for using sqoop in Java Program for importing data from MySQL to HDFS/HBase. Make sure you have sqoop jar in your classpath:
SqoopOptions options = new SqoopOptions(); options.setConnectString("jdbc:mysql://HOSTNAME:PORT/DATABASE_NAME"); //options.setTableName("TABLE_NAME"); //options.setWhereClause("id>10"); // this where clause works when importing whole table, ie when setTableName() is used options.setUsername("USERNAME"); options.setPassword("PASSWORD"); //options.setDirectMode(true); // Make sure the direct mode is off when importing data to HBase options.setNumMappers(8); // Default value is 4 options.setSqlQuery("SELECT * FROM user_logs WHERE $CONDITIONS limit 10"); options.setSplitByCol("log_id"); // HBase options options.setHBaseTable("HBASE_TABLE_NAME"); options.setHBaseColFamily("colFamily"); options.setCreateHBaseTable(true); // Create HBase table, if it does not exist options.setHBaseRowKeyColumn("log_id"); int ret = new ImportTool().run(options);
As suggested by Harel, we can use the output of the run() method for error handling. Hoping this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With