Hadoop 為 Apache 基金會的開源頂級專案,為軟體框架做為分散式儲存及運算,無論是增減加機器都能處理,另具備高可用性、數據副本等能力
機器基本訊息:
- 準備五台機器 (兩台主節點、三台工作節點)
IP |
FQDN |
HOSTNAME |
用途 |
192.168.1.30 |
test30.example.org |
test30 |
Master 節點 (Namenode) |
192.168.1.31 |
test31.example.org |
test31 |
Master 節點 (ResourceManager) |
192.168.1.32 |
test32.example.org |
test32 |
Worker 節點 |
192.168.1.33 |
test33.example.org |
test33 |
Worker 節點 |
192.168.1.34 |
test34.example.org |
test34 |
Worker 節點 |
-
OS : Ubuntu 18.04
-
資源配置 :
- Cpu : 4 core
- Ram : 8 G
- Disk : 50 G
建置步驟 - Spark + Jupyter 應用程式安裝:
執行前,請先確認叢集均已啟動 hdfs 及 yarn 服務
1. 下載及安裝Spark(管理者身份)
- 下載
1
2
|
cd
wget http://ftp.tc.edu.tw/pub/Apache/spark/spark-2.4.4/spark-2.4.4-bin-hadoop2.7.tgz
|
- 解壓縮
1
|
tar -xvf spark-2.4.4-bin-hadoop2.7.tgz -C /usr/local
|
- 更名
1
|
mv /usr/local/spark-2.4.4-bin-hadoop2.7 /usr/local/spark
|
- 修改spark資料夾及檔案使用者
1
|
chown -R hadoop:hadoop /usr/local/spark
|
2. 修改Spark環境變數(hadoop身份)
- 設定.bashrc

- 重新載入設定檔
1
|
source ~/.bashrc #( . .bashrc)
|
- 查看環境變數

3. 更改 Spark運行程式時環境腳本(hadoop身份)
- 複製並建立一份spark-env腳本
1
|
cp /usr/local/spark/conf/spark-env.sh.template /usr/local/spark/conf/spark-env.sh
|
- 編輯spark-env腳本
1
|
nano /usr/local/spark/conf/spark-env.sh
|

4. 跑個pi 測試一下Spark(hadoop身份)
1
2
3
4
5
6
7
8
9
10
11
|
cd $SPARK_HOME
./bin/spark-submit --class org.apache.spark.examples.SparkPi \
--master yarn \
--deploy-mode cluster \
--driver-memory 1g \
--executor-memory 1g \
--executor-cores 1 \
--num-executors 3 \
--queue default \
examples/jars/spark-examples*.jar \
100
|




- 明顯看出Spark 遠遠勝過 MapReduce (同樣跑pi 100次)
MapReduce花費3分11秒
Spark花費14秒
5. 停止Spark運行程式時都要上傳jar檔到hdfs方式(hadoop身份)


- 在 hdfs 建立目錄放jar檔
1
2
3
|
hdfs dfs -mkdir -p /user/spark/share/jars
hdfs dfs -put $SPARK_HOME/jars/* /user/spark/share/jars/
hdfs dfs -ls /user/spark/share/jars
|
- 上傳jar檔到hdfs
1
2
|
hdfs dfs -mkdir -p /user/spark/share/jars
hdfs dfs -put $SPARK_HOME/jars/* /user/spark/share/jars/
|

- 確認jar檔都上傳
1
|
hdfs dfs -ls /user/spark/share/jars | wc -l
|

- 編輯spark-defaults.conf 的hdfs路徑
1
2
|
cp /usr/local/spark/conf/spark-defaults.conf.template /usr/local/spark/conf/spark-defaults.conf
nano /usr/local/spark/conf/spark-defaults.conf
|

- 跑個pi 檢測一下

出現一堆的Not copying就代表成功了
)
時間減少1秒
6. 使用PySpark shell(hadoop身份)
- 使用Spark的readme當範本測試一下

- 開啟pyspark shell
1
2
|
cd $SPARK_HOME
./bin/pyspark --master yarn --deploy-mode client --num-executors 1 --executor-cores 1
|


- 運行程式看看

7. 安裝jupter 系列及pyspark 等套件(管理者身份)
安裝失敗
如果沒有安裝 python 3開發工具箱,會導致失敗
請執行下方步驟:
- 安裝python開發工具箱
1
2
|
sudo apt update
sudo apt install python3-dev
|
- 安裝pip
1
2
3
|
#取得最新版pip腳本
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
|
- 安裝pyspark 套件
- 安裝jupter 系列套件
1
|
pip3 install jupyterlab
|
8. jupyter 系列遠端使用及產生密碼(一般使用者身份)
- 創建jupyter設定檔
1
|
jupyter notebook --generate-config
|

- 修改設定檔
1
|
nano .jupyter/jupyter_notebook_config.py
|
- 將登入網域開成全域
1
|
c.NotebookApp.ip = '0.0.0.0'
|

- 產生密碼
1
|
jupyter notebook password
|

- 開啟筆記本或是Lab
1
|
jupyter notebook #jupyter lab
|
Success
就可以藉由瀏覽器登入


Question
或是用手機登入coding…瘋掉拉
