我创建了一个新的Java项目,然后我添加了Library Sqoop和Hadoop。 (图书馆是“hadoop-core-1.1.1.jar,sqoop-1.4.2.jar等......”。)
然后我尝试了以下代码:
public class MySqoopDriver {
public static void main(String[] args) {
String[] str = { "export", "--connect", "jdbc:mysql://localhost/mytestdb", "--hadoop-home",
"/home/yoonhok/development/hadoop-1.1.1", "--table", "tbl_1", "--export-dir", "hdfs://localhost:9000/user/hive/warehouse/tbl_1",
"--username", "yoonhok", "--password", "1234"};
Sqoop.runTool(str);
}
}
参数是正确的,因为当我在终端尝试时,它运作良好。
但它没有用。错误消息是:
13/02/17 16:23:07 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P instead.
13/02/17 16:23:07 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
13/02/17 16:23:07 INFO tool.CodeGenTool: Beginning code generation
13/02/17 16:23:07 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `tbl_1` AS t LIMIT 1
13/02/17 16:23:07 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM `tbl_1` AS t LIMIT 1
13/02/17 16:23:07 INFO orm.CompilationManager: HADOOP_HOME is /home/yoonhok/development/hadoop-1.1.1
Note: /tmp/sqoop-yoonhok/compile/86a3cab62184ad50a3ae11e7cb0e4f4d/tbl_1.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
13/02/17 16:23:08 INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-yoonhok/compile/86a3cab62184ad50a3ae11e7cb0e4f4d/tbl_1.jar
13/02/17 16:23:08 INFO mapreduce.ExportJobBase: Beginning export of tbl_1
13/02/17 16:23:09 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
13/02/17 16:23:09 INFO input.FileInputFormat: Total input paths to process : 1
13/02/17 16:23:09 INFO input.FileInputFormat: Total input paths to process : 1
13/02/17 16:23:09 INFO mapred.JobClient: Cleaning up the staging area file:/tmp/hadoop-yoonhok/mapred/staging/yoonhok1526809600/.staging/job_local_0001
13/02/17 16:23:09 ERROR security.UserGroupInformation: PriviledgedActionException as:yoonhok cause:java.io.FileNotFoundException: File /user/hive/warehouse/tbl_1/000000_0 does not exist.
13/02/17 16:23:09 ERROR tool.ExportTool: Encountered IOException running export job: java.io.FileNotFoundException: File /user/hive/warehouse/tbl_1/000000_0 does not exist.
当我检查HDFS时,文件存在:
hadoop fs -ls /user/hive/warehouse/tbl_1
Found 1 items
-rw-r--r-- 1 yoonhok supergroup 240 2013-02-16 18:56 /user/hive/warehouse/tbl_1/000000_0
如何在Java程序中执行Sqoop?
我尝试过Processbuilder和Process,但我不想使用它们。
我真的想使用Sqoop API,但我听说它还不存在。
我看了this question,但它对我不起作用。
答案 0 :(得分:2)
有一个技巧对我很有帮助。通过ssh,您可以直接执行Sqoop命令。您必须使用的是SSH Java库
您必须按照此步骤操作。
下载sshxcute java库:https://code.google.com/p/sshxcute/ 并将其添加到包含以下Java代码
的java项目的构建路径中import net.neoremind.sshxcute.core.SSHExec;
import net.neoremind.sshxcute.core.ConnBean;
import net.neoremind.sshxcute.task.CustomTask;
import net.neoremind.sshxcute.task.impl.ExecCommand;
public class TestSSH {
public static void main(String args[]) throws Exception{
// Initialize a ConnBean object, parameter list is ip, username, password
ConnBean cb = new ConnBean("192.168.56.102", "root","hadoop");
// Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a singleton SSHExec instance
SSHExec ssh = SSHExec.getInstance(cb);
// Connect to server
ssh.connect();
CustomTask sampleTask1 = new ExecCommand("echo $SSH_CLIENT"); // Print Your Client IP By which you connected to ssh server on Horton Sandbox
System.out.println(ssh.exec(sampleTask1));
CustomTask sampleTask2 = new ExecCommand("sqoop import --connect jdbc:mysql://192.168.56.101:3316/mysql_db_name --username=mysql_user --password=mysql_pwd --table mysql_table_name --hive-import -m 1 -- --schema default");
ssh.exec(sampleTask2);
ssh.disconnect();
}
}
答案 1 :(得分:1)
首先让我提一下Sqoop 1没有官方客户端API。即使以某种方式调用Sqoop也很常见且有效。
基于日志,我猜你正在执行Sqoop的java应用程序在类路径上没有hadoop配置。因此,Sqoop不会获得有关您的群集的信息,并将以“本地”模式工作。您需要将hadoop配置放入类路径中,以便针对远程集群运行Sqoop。有关详细信息,请在stackoverflow上的entry后面结帐。
答案 2 :(得分:1)
您可以使用“SqoopOptions”在Java程序中执行sqoop。
这是将MySql中的表导入HDFS的示例代码。
public static void importSQLToHDFS() throws Exception {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
Configuration config = new Configuration();
config.addResource(new Path("/.../conf/core-site.xml"));
config.addResource(new Path("/.../conf/hdfs-site.xml"));
properties.load(new FileInputStream("/.../sqoopimport.properties"));
SqoopOptions options = new SqoopOptions();
options.setDriverClassName(driver);
options.setHadoopHome("/.../hadoop-0.20.2-cdh3u2");
options.setConnectString(properties.getProperty("db_connection_string"));
options.setTableName(properties.getProperty("db_mysql_table_name"));
options.setUsername(properties.getProperty("db_usr_id"));
options.setPassword(properties.getProperty("db_passwd"));
options.setNumMappers(1);
options.setTargetDir(properties.getProperty("path_export_file"));
options.setFileLayout(FileLayout.TextFile);
new ImportTool().run(options);
}
对于导出,请参阅下面的示例代码。 注意:此处不使用属性文件。确保您已创建要导入数据的表。
public static boolean exportHDFSToSQL() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
try {
SqoopOptions options=new SqoopOptions();
options.setConnectString("jdbc:mysql://localhost:3306/dbName");
options.setUsername("user_name");
options.setPassword("pwd");
options.setExportDir("path of file to be exported from hdfs");
options.setTableName("table_name");
options.setInputFieldsTerminatedBy(',');
options.setNumMappers(1);
new ExportTool().run(options);
} catch (Exception e) {
return false;
}
return true;
}