Java代码执行sqoop抽取动作
2021/6/4 14:23:10
本文主要是介绍Java代码执行sqoop抽取动作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
执行从MySQL数据库导出数据到HDFS上。
执行前需准备:
在eclipse中写的代码,所以,又新加了sqoop-1.4.6-cdh5.5.2.jar放入原有的hadoop项目中,否则会有编译错误。
同时,在集群上,将此包加入HADOOP_HOME/share/hadoop/yarn下,否则会报缺少Jar包的错误。
错误显示:
[hadoop@fa01 ~]$ hadoop jar SqoopTest1.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/sqoop/util/OptionsFileUtil
代码:
import org.apache.hadoop.conf.Configuration;
import org.apache.sqoop.Sqoop;
import org.apache.sqoop.tool.SqoopTool;
import org.apache.sqoop.util.OptionsFileUtil;
public class SqoopTest1 {
private static int importDataFromMysql() throws Exception {
String[] args = new String[] {
//参数可以继续添加
"--connect", "jdbc:mysql://192.20.1.54:3306/tbname_test",
"--driver", "com.mysql.jdbc.Driver",
"--username", "username",
"--password", "123456",
"--table", "abtest_data",
"-m", "1",
"--target-dir", "/user/hadoop/java_sqoop_test1"
};
String[] expandArguments = OptionsFileUtil.expandArguments(args);
SqoopTool tool = SqoopTool.getTool("import");
Configuration conf = new Configuration();
conf.set("fs.default.name", "hdfs://192.168.20.32:9000");
Configuration loadPlugins = SqoopTool.loadPlugins(conf);
Sqoop sqoop = new Sqoop((com.cloudera.sqoop.tool.SqoopTool) tool, loadPlugins);
return Sqoop.runSqoop(sqoop, expandArguments);
}
public static void main(String[] args) throws Exception {
importDataFromMysql();
}
}
参考:
http://blog.csdn.net/shantaodaoshik852/article/details/77266831
http://blog.csdn.net/guzicheng/article/details/41519947
转自:https://blog.51cto.com/feature09/1966296
这篇关于Java代码执行sqoop抽取动作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略