Managed Service for Apache Spark 選用 Jupyter 元件

Jupyter 選用元件可讓您在 Managed Service for Apache Spark 叢集上,執行互動式資料分析筆記本。這項元件提供網頁式單一使用者 Jupyter 筆記本環境,支援 JupyterLab 網頁版 UI。

多使用者筆記本替代方案

如要為多位使用者提供筆記本服務,請建立支援 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 元件閘道。

Google Cloud 控制台

  1. 在 Google Cloud 控制台,開啟「Create cluster」(建立叢集) 頁面。
  2. 按一下「其他設定」展開該部分。
  3. 編輯「Optional components」(選用元件)。
  4. 在開啟的面板中,勾選「Jupyter Notebook」核取方塊,然後按一下「Save」(儲存)。

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 元件可以透過 Dataproc 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 的機器學習情境