简体   繁体   中英

Sqoop from within a Java program

I have read the questions on using Sqoop from within a Java program here , here and here .

I came up with the following, but I am stumped by a ClassNotFoundException:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import com.cloudera.sqoop.SqoopOptions;
import com.cloudera.sqoop.SqoopOptions.FileLayout;

import com.mysql.jdbc.*;

public class SqoopExample {
    public static void main(String[] args) throws Exception {
        String driver = "com.mysql.jdbc.driver";
        Class.forName(driver);

        Configuration config = new Configuration();
        // note that this is HDFS path, rather than core path
        config.addResource(new Path("./config/core-site.xml"));
        config.addResource(new Path("./config/hdfs-site.xml"));
        FileSystem dfs = FileSystem.get(config);    

        SqoopOptions options = new SqoopOptions();
        options.setDriverClassName(driver);
        options.setConnectString("jdbc:mysql://localhost:3306/dbname");
        options.setTableName("v_webstats");
        options.setUsername("root");
        options.setNumMappers(1);

        options.setTargetDir(dfs.getWorkingDirectory()+"/TestDirectory");
        options.setFileLayout(FileLayout.TextFile);

        new com.cloudera.sqoop.tool.ImportTool().run(options);
    }
}

I am sure that all the jars are correctly included and I have tested the connection to my database server and that works as well.

The exact error is:

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at SqoopExample.main(SqoopExample.java:14)

not sure what I am doing wrong.

I believe that proper name of the MySQL JDBC driver class is:

com.mysql.jdbc.Driver

(notice the capital "D" in the "Driver)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM