叢集建立與管理

使用 Python SDK 的 Databricks

Avi Steinberg

Senior Software Engineer

無伺服器 vs. 受管基礎設施

無伺服器

  • 在 Databricks 全代管的基礎設施上執行
  • 專注在程式碼,不必管基礎設施
  • 依需求付費使用運算資源

受管

  • 擁有更多組態控制權
  • 長時程作業或可預測工作負載更省成本
使用 Python SDK 的 Databricks

建立 Databricks Spark 叢集

from databricks.sdk import WorkspaceClient

w = WorkspaceClient()
cluster = w.clusters.create(
    cluster_name="datacamp-cluster-name",

spark_version="latest",
autotermination_minutes=20,
num_workers=3, ).result()
1 https://databricks-sdk-py.readthedocs.io/en/latest/workspace/compute/clusters.html
使用 Python SDK 的 Databricks

列出叢集

from databricks.sdk import WorkspaceClient
# Instantiate WorkspaceClient
w = WorkspaceClient()

# Print id of each cluster in workspace
clusters = w.clusters.list()
for cluster in clusters:
    print(f"ClusterId={cluster.cluster_id}")

輸出:

ClusterId=0113-13328-woj98c32
1 https://databricks-sdk-py.readthedocs.io/en/latest/workspace/compute/clusters.html
使用 Python SDK 的 Databricks

啟動叢集

from databricks.sdk import WorkspaceClient
import os

w = WorkspaceClient()
cluster_id=os.environ["DATABRICKS_CLUSTER_ID"]

# Start cluster with id stored in cluster_id variable
try:
  w.clusters.start(cluster_id=cluster_id).result()
except: 
  print(f"Cannot start cluster_id={cluster_id} because it is already running")
1 https://databricks-sdk-py.readthedocs.io/en/latest/workspace/compute/clusters.html
使用 Python SDK 的 Databricks

檢查叢集狀態

from databricks.sdk import WorkspaceClient
import os

w = WorkspaceClient()

# Print state of cluster
cluster_info = w.clusters.get(cluster_id=os.environ["DATABRICKS_CLUSTER_ID"])
print(f"cluster state={cluster_info.state}")

輸出:

cluster state=State.RUNNING
1 https://databricks-sdk-py.readthedocs.io/en/latest/workspace/compute/clusters.html
使用 Python SDK 的 Databricks

刪除叢集

from databricks.sdk import WorkspaceClient
import os

w = WorkspaceClient()
# Delete databricks cluster, with id stored in an environment variable
w.clusters.delete(cluster_id=os.environ["DATABRICKS_CLUSTER_ID"])

cluster_info = w.clusters.get(cluster_id=os.environ["DATABRICKS_CLUSTER_ID"]) print(f"cluster state={cluster_info.state}")
輸出: 
cluster state=State.TERMINATED
使用 Python SDK 的 Databricks

一起來練習吧!

使用 Python SDK 的 Databricks

Preparing Video For Download...