使用 Spark 的「Hadoop Free」建置

Spark 使用 Hadoop 函式庫來處理 HDFS 和 YARN。從 Spark 1.4 版開始,專案封裝了「Hadoop Free」建置,讓您可以更輕鬆地將單一 Spark 二進位檔連接到任何 Hadoop 版本。若要使用這些建置,您需要修改 SPARK_DIST_CLASSPATH 以包含 Hadoop 的套件 JAR 檔。最方便的做法是在 conf/spark-env.sh 中新增一則項目。

此頁面說明如何將 Spark 連接到 Hadoop,以適用於不同類型的發行版。

Apache Hadoop

對於 Apache 發行版,您可以使用 Hadoop 的「classpath」指令。例如

### in conf/spark-env.sh ###

# If 'hadoop' binary is on your PATH
export SPARK_DIST_CLASSPATH=$(hadoop classpath)

# With explicit path to 'hadoop' binary
export SPARK_DIST_CLASSPATH=$(/path/to/hadoop/bin/hadoop classpath)

# Passing a Hadoop configuration directory
export SPARK_DIST_CLASSPATH=$(hadoop --config /path/to/configs classpath)

在 Kubernetes 上執行 Spark 的 Hadoop Free 建置設定

要在 Kubernetes 上執行 Spark 的 Hadoop Free 建置,執行器映像檔必須具備適當版本的 Hadoop 二進位檔和正確的 SPARK_DIST_CLASSPATH 值設定。請參閱以下範例,以了解在執行器 Dockerfile 中所需的相關變更

### Set environment variables in the executor dockerfile ###

ENV SPARK_HOME="/opt/spark"  
ENV HADOOP_HOME="/opt/hadoop"  
ENV PATH="$SPARK_HOME/bin:$HADOOP_HOME/bin:$PATH"  
...  

#Copy your target hadoop binaries to the executor hadoop home   

COPY /opt/hadoop3  $HADOOP_HOME  
...

#Copy and use the Spark provided entrypoint.sh. It sets your SPARK_DIST_CLASSPATH using the hadoop binary in $HADOOP_HOME and starts the executor. If you choose to customize the value of SPARK_DIST_CLASSPATH here, the value will be retained in entrypoint.sh

ENTRYPOINT [ "/opt/entrypoint.sh" ]
...