您好,登錄后才能下訂單哦!
執(zhí)行從MySQL數(shù)據(jù)庫導(dǎo)出數(shù)據(jù)到HDFS上。
執(zhí)行前需準備:
在eclipse中寫的代碼,所以,又新加了sqoop-1.4.6-cdh6.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[] {
//參數(shù)可以繼續(xù)添加
"--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
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。