การสร้างและจัดการคลัสเตอร์

Databricks กับ Python SDK

Avi Steinberg

Senior Software Engineer

Serverless vs. Managed Infrastructure

Serverless

  • รันบนโครงสร้างพื้นฐานที่ Databricks จัดการให้ทั้งหมด
  • โฟกัสที่โค้ดแทนที่จะเป็น infrastructure
  • จ่ายค่า compute ตามการใช้งานจริง

Managed

  • ควบคุมการตั้งค่าได้มากขึ้น
  • คุ้มค่ากว่าสำหรับงานที่รันนาน หรือ workload ที่คาดเดาได้
Databricks กับ Python SDK

สร้าง 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
Databricks กับ Python SDK

แสดงรายการคลัสเตอร์

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}")

Output:

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

เริ่มต้นคลัสเตอร์

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
Databricks กับ Python SDK

ตรวจสอบสถานะของคลัสเตอร์

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}")

Output:

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

ลบคลัสเตอร์

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}")
Output: 
cluster state=State.TERMINATED
Databricks กับ Python SDK

มาฝึกกันเถอะ!

Databricks กับ Python SDK

Preparing Video For Download...