sqoop如何指定pg庫的模式(方法詳解)
sqoop是一款用于hadoop和關(guān)系型數(shù)據(jù)庫之間數(shù)據(jù)導(dǎo)入導(dǎo)出的工具。你可以通過sqoop把數(shù)據(jù)從數(shù)據(jù)庫(比如mysql,oracle)導(dǎo)入到hdfs中;也可以把數(shù)據(jù)從hdfs中導(dǎo)出到關(guān)系型數(shù)據(jù)庫中。sqoop通過Hadoop的MapReduce導(dǎo)入導(dǎo)出,因此提供了很高的并行性能以及良好的容錯(cuò)性。

sqoop適合以下的人群使用:
- 系統(tǒng)和應(yīng)用開發(fā)者
- 系統(tǒng)管理員
- 數(shù)據(jù)庫管理員
- 數(shù)據(jù)分析師
- 數(shù)據(jù)工程師
說明
使用sqoop導(dǎo)出導(dǎo)入數(shù)據(jù)非常的方便,但是對(duì)于postgresql(簡(jiǎn)稱PG庫)時(shí)就碰到了一個(gè)問題,pg庫是三層結(jié)構(gòu)的database——schema——table。如果想導(dǎo)入到某一個(gè)模式下,那就需要指定模式才可以。但是sqoop如何指定pg庫的模式?
解決辦法
碰到問題首先要看文檔才對(duì)的。文檔這里已經(jīng)指出如何指定pg庫的schema了。官方文檔地址
文檔已經(jīng)說了,如果向指定schema需要添加-- --schema <name> 但是要注意的是必須在命令行的?。。?!最后?。?!添加才會(huì)生效。

但是,這是命令行的解決辦法,如果我們使用的是java呢?在沒解決之前,我的java代碼是這樣寫的:
public static boolean ExportCmdInPg(Configuration conf, String tableName, List<String> columns, String hdfsDir,Map<String, String> dbMap) {
try {
LogUtils.logInfoPrint("開始任務(wù)",logger);
List<String> list = new ArrayList<>();
list.add("--connect");
list.add(dbMap.get(Constant.DRIVERURL));
list.add("--username");
list.add(dbMap.get(Constant.USER));
list.add("--password");
list.add(dbMap.get(Constant.PASSWORD));
list.add("--table");
list.add(tableName);
list.add("--columns");
list.add(StringUtils.join(columns, ','));
list.add("--fields-terminated-by");
list.add("\t");
list.add("--export-dir");
list.add(hdfsDir);
list.add("-m");
list.add("1");
ExportTool exporter = new ExportTool();
Sqoop sqoop = new Sqoop(exporter);
String[] data = list.toArray(new String[0]);
if (0 == data.length) {
LogUtils.logErrorPrint("sqoop參數(shù)為空,請(qǐng)檢查ExportCmd方法!",logger);
return false;
}
if (0 == Sqoop.runSqoop(sqoop, data)){
return true;
}
}catch (Exception e){
LogUtils.logErrorPrint("ExportCmd 導(dǎo)入到HDFS出現(xiàn)錯(cuò)誤",logger,e);
}
return false;
}結(jié)果當(dāng)然是不成功。那我哦添加-- --schema 參數(shù)試一下
public static boolean ExportCmdInPg(Configuration conf, String tableName, List<String> columns, String hdfsDir,Map<String, String> dbMap) {
try {
LogUtils.logInfoPrint("開始任務(wù)",logger);
List<String> list = new ArrayList<>();
list.add("--connect");
list.add(dbMap.get(Constant.DRIVERURL));
list.add("--username");
list.add(dbMap.get(Constant.USER));
list.add("--password");
list.add(dbMap.get(Constant.PASSWORD));
list.add("--table");
list.add(tableName);
list.add("--columns");
list.add(StringUtils.join(columns, ','));
list.add("--fields-terminated-by");
list.add("\t");
list.add("--export-dir");
list.add(hdfsDir);
list.add("-m");
list.add("1");
list.add("-- --schema");
list.add("HERO");
ExportTool exporter = new ExportTool();
Sqoop sqoop = new Sqoop(exporter);
String[] data = list.toArray(new String[0]);
if (0 == data.length) {
LogUtils.logErrorPrint("sqoop參數(shù)為空,請(qǐng)檢查ExportCmd方法!",logger);
return false;
}
if (0 == Sqoop.runSqoop(sqoop, data)){
return true;
}
}catch (Exception e){
LogUtils.logErrorPrint("ExportCmd 導(dǎo)入到HDFS出現(xiàn)錯(cuò)誤",logger,e);
}
return false;
}結(jié)果也是不成功,顯示報(bào)錯(cuò)不識(shí)別-- --schema 。。。為了能夠使schema參數(shù)生效,廢了我不少勁。。。也查了不少資料,但是查到的資料都沒有關(guān)于java的schema的設(shè)置。所以。。。最終正確的解決辦法是:
public static boolean ExportCmdInPg(Configuration conf, String tableName, List<String> columns, String hdfsDir,Map<String, String> dbMap) {
try {
LogUtils.logInfoPrint("開始sqoop將oracle的數(shù)據(jù)導(dǎo)出到HDFS目錄",logger);
List<String> list = new ArrayList<>();
list.add("--connect");
list.add(dbMap.get(Constant.DRIVERURL));
list.add("--username");
list.add(dbMap.get(Constant.USER));
list.add("--password");
list.add(dbMap.get(Constant.PASSWORD));
list.add("--table");
list.add(tableName);
list.add("--columns");
list.add(StringUtils.join(columns, ','));
list.add("--fields-terminated-by");
list.add("\t");
list.add("--export-dir");
list.add(hdfsDir);
list.add("-m");
list.add("1");
// 注意這里是--是分開的,源碼這里是通過--做判斷的
list.add("--");
list.add("--schema");
list.add("HERO");
ExportTool exporter = new ExportTool();
Sqoop sqoop = new Sqoop(exporter);
String[] data = list.toArray(new String[0]);
if (0 == data.length) {
LogUtils.logErrorPrint("sqoop參數(shù)為空,請(qǐng)檢查ExportCmd方法!",logger);
return false;
}
if (0 == Sqoop.runSqoop(sqoop, data)){
return true;
}
}catch (Exception e){
LogUtils.logErrorPrint("ExportCmd 導(dǎo)入到HDFS出現(xiàn)錯(cuò)誤",logger,e);
}
return false;
}當(dāng)然你也可能會(huì)使用字符串?dāng)?shù)組,數(shù)組方式就要這樣寫了
// 這里只是舉個(gè)示例
String[] string = new String[]{"--","--schema","HERO"}so 問題解決,心情愉快。如果問題不解決,可能會(huì)憋一天。。。。
本文分享自華為云社區(qū)《【Hadoop】關(guān)于Sqoop導(dǎo)出數(shù)據(jù)到postgresql時(shí)schema的設(shè)置問題》,作者:Copy工程師 。
到此這篇關(guān)于sqoop如何指定pg庫的模式的文章就介紹到這了,更多相關(guān)sqoop指定pg庫的模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
45個(gè)GIT經(jīng)典操作場(chǎng)景使用詳解
這篇文章主要介紹了45個(gè)GIT經(jīng)典操作場(chǎng)景使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
解決IDEA中g(shù)it拉取代碼時(shí)出現(xiàn)Update canceled問題
這篇文章主要介紹了解決IDEA中g(shù)it拉取代碼時(shí)出現(xiàn)Update canceled問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
使用SSH快速下載Git項(xiàng)目的實(shí)現(xiàn)方法
下面小編就為大家分享一篇使用SSH快速下載Git項(xiàng)目的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
Git如何實(shí)現(xiàn)撤銷提交(命令行+IDEA)
這篇文章主要介紹了Git如何實(shí)現(xiàn)撤銷提交(命令行+IDEA)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

