클러스터 생성 및 관리

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

Output:

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

Output:

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}")
Output: 
cluster state=State.TERMINATED
Python SDK로 Databricks 다루기

연습해 봅시다!

Python SDK로 Databricks 다루기

Preparing Video For Download...