Spark實(shí)例-spark讀取外部配置文件之--files

在運(yùn)行spark程序的時(shí),有時(shí)需要讀取外部配置參數(shù),比如mysql的host參數(shù)、端口號(hào)、es主機(jī)ip、es端口號(hào)等。通過(guò)外部文件配置參數(shù)也方便程序遷移。下面就來(lái)看看如何來(lái)實(shí)現(xiàn)。

1、首先我們需要一個(gè)配置文件:
config.properties


微信截圖_20190425173522.png

2、上傳配置文件到某個(gè)節(jié)點(diǎn):


微信截圖_20190425173652.png

3、然后需要在程序里加上如下代碼:
val props = new Properties()
props.load(new FileInputStream("config.properties"))
props.getProperty("hdfsURL")
props.getProperty("zookeeperQuorum")
props.getProperty("brokers")
props.getProperty("mysqlURL_db")
props.getProperty("table")
props.getProperty("db_username")
props.getProperty("db_password")

4、通過(guò) Maven打包程序:test_CDH.jar

5、上傳 test_CDH.jar 到集群

6、執(zhí)行 submit 命令

spark2-submit --master yarn --deploy-mode cluster --files config.properties --jars libs/hbase-client-1.2.0.jar,libs/hbase-common-1.2.0.jar,libs/hbase-server-1.2.0.jar,libs/mysql-connector-java-8.0.15.jar --driver-class-path libs/mysql-connector-java-8.0.15.jar --conf spark.executor.userClassPathFirst=true --conf spark.driver.userClassPathFirst=true --class test_CDH.Main libs/test_CDH.jar $1

--files 參數(shù)指定我們需要加載的外部配置文件

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容