Managed Service for Apache Spark 選用 Jupyter 元件

使用選用元件功能建立 Managed Service for Apache Spark 叢集時,可以安裝 Jupyter 等其他元件。本頁說明 Jupyter 元件的相關資訊。

Jupyter 元件是用於進行互動資料分析的網頁式單一使用者筆記本,支援 JupyterLab 網頁版使用者介面。Jupyter 網頁版使用者介面可透過叢集第一個主節點的連接埠 8123 存取。

為多位使用者啟動筆記本。您可以建立支援 Managed Service for Apache Spark 的 Vertex AI Workbench 執行個體,或在 VM 上安裝 Managed Service for Apache Spark JupyterLab 外掛程式,為多位使用者提供筆記本服務。

設定 Jupyter。您可以藉由配置 dataproc:jupyter 叢集屬性來設定 Jupyter。為避免未加密的筆記本伺服器 API 成為遠端執行程式碼的漏洞,系統預設將 dataproc:jupyter.listen.all.interfaces 叢集屬性設定為 false,在啟用元件閘道時,會將連線限制於 localhost (127.0.0.1) (安裝 Jupyter 元件時必須啟用元件閘道)。

Jupyter 筆記本提供了 Python 核心,用來執行 Spark 程式碼和 PySpark 核心。根據預設,筆記本會儲存在 Cloud Storage 中,位於叢集建立期間由使用者指定或系統自動建立的 Managed Service for Apache Spark 暫存 bucket。在建立叢集時,可以使用 dataproc:jupyter.notebook.gcs.dir 叢集屬性來變更位置。

處理資料檔案。Jupyter 筆記本可以用來處理已上傳至 Cloud Storage 的資料檔案。 由於 Cloud Storage 連接器已預先安裝在 Managed Service for Apache Spark 叢集上,因此您可以直接從筆記本中參考檔案。以下範例說明如何存取 Cloud Storage 中的 CSV 檔案:

df = spark.read.csv("gs://bucket/path/file.csv")
df.show()

如需 PySpark 範例,請參閱「一般載入和儲存功能」。

安裝 Jupyter

請在建立 Managed Service for Apache Spark 叢集時安裝元件。Jupyter 元件需要啟用 Managed Service for Apache Spark Component Gateway

控制台

  1. 啟用元件。
    • 在 Google Cloud 控制台中,開啟 Managed Service for Apache Spark 的「建立叢集」頁面。系統會選取「Set up cluster」(設定叢集) 面板。
    • 在「Components」(元件) 部分執行下列操作:
      • 在「Optional components」(選用元件) 下方選取「Jupyter」元件。
      • 在「Component Gateway」(元件閘道) 下方,選取「Enable component gateway」(啟用元件閘道) (請參閱「查看及存取元件閘道網址」)。

gcloud CLI

如要建立包含 Jupyter 元件的 Managed Service for Apache Spark 叢集,請使用 gcloud dataproc clusters create cluster-name 指令並加上 --optional-components 旗標。

最新預設映像檔版本範例

以下範例將 Jupyter 元件安裝於使用最新預設映像檔版本的叢集上。

gcloud dataproc clusters create cluster-name \
    --optional-components=JUPYTER \
    --region=region \
    --enable-component-gateway \
    ... other flags

REST API

Jupyter 元件可以透過 Managed Service for Apache Spark API 安裝,方法是在 clusters.create 要求中使用 SoftwareConfig.Component

開啟 Jupyter 和 JupyterLab 使用者介面

按一下Google Cloud 控制台的「Component Gateway」(元件閘道) 連結,即可在本機瀏覽器開啟叢集主要節點所執行的 Jupyter Notebook 或 JupyterLab 使用者介面。

選取「GCS」或「Local Disk」(本機磁碟),即可在任一位置建立新的 Jupyter Notebook。

將 GPU 連接至主要節點和 worker 節點

使用 Jupyter Notebook 時,可以將 GPU 新增至叢集的主要節點和 worker 節點,以便:

  1. 在 Spark 中預先處理資料,再將 DataFrame 收集到主要執行個體上,並執行 TensorFlow
  2. 使用 Spark 調度管理 TensorFlow 的平行運作
  3. 執行 Tensorflow-on-YARN
  4. 應用於其他需要 GPU 的機器學習情境