Creazione e gestione dei job

Databricks con il Python SDK

Avi Steinberg

Senior Software Engineer

Percorso di un Notebook Databricks

  • I job Databricks possono eseguire codice in un Notebook Databricks
  • WorkspaceClient.current_user.me().user_name ottiene l'username dell'utente connesso
  • Il percorso di un notebook è /Users/${username}/${notebook_name}
# Supponi che nel workspace ci sia un notebook chiamato My_Notebook
w = WorkspaceClient()
notebook_path = f'/Users/{w.current_user.me().user_name}/My_Notebook'
Databricks con il Python SDK

Creare un job Databricks

WorkspaceClient.jobs.create(createParams)

Passa i parametri name e tasks per descrivere il job da creare

createParams:
  name: str
  tasks: List[Task]
Task:
  description=str
  notebook_task=NotebookTask
  task_key=str
NotebookTask:
  notebook_path: str
1 https://databricks-sdk-py.readthedocs.io/en/latest/workspace/jobs/jobs.html
Databricks con il Python SDK

Creare ed eseguire un job Databricks

from databricks.sdk import WorkspaceClient

# Crea il percorso verso il notebook "My_Notebook"
w = WorkspaceClient()
notebook_path = f'/Users/{w.current_user.me().user_name}/My_Notebook'

# Crea un job che esegue Datacamp_Test_Notebook new_job = w.jobs.create(name='sdk-dc-project-task', tasks=[jobs.Task( description="create_notebook_test", notebook_task=jobs.NotebookTask(notebook_path=notebook_path), task_key="my-key") ]) print(f"New Job Id={new_job.job_id}")
w.jobs.run_now(job_id=new_job.job_id).result() # Esegui il job creato
1 https://docs.databricks.com/en/dev-tools/sdk-python.html
Databricks con il Python SDK

Elencare i job Databricks

from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
jobs = w.jobs.list()
for job in jobs:
    print(f"JobId={job.job_id}")
JobId=888763141802192
JobId=37050453972815
JobId=681550316180975
JobId=629994089852037
Databricks con il Python SDK

Eliminare un job Databricks

WorkspaceClient.jobs.delete(job_id: str)
1 1. https://databricks-sdk-py.readthedocs.io/en/latest/workspace/jobs/jobs.html
Databricks con il Python SDK

Eliminare un job Databricks

# Pre-requisito: crea il notebook Databricks
# nel cluster Databricks
w = WorkspaceClient()
notebook_path = f'/Users/{w.current_user.me().user_name}/My_Notebook'
# Crea un job che esegue Datacamp_Test_Notebook
new_job = w.jobs.create(name='sdk-dc-project-task',
                            tasks=[
                              jobs.Task(description="create_notebook_test",
                                        existing_cluster_id=cluster_id,
                      notebook_task=jobs.NotebookTask(notebook_path=notebook_path),
                                        task_key="my-key")
                            ])

w.jobs.delete(job_id=new_job.job_id)
1 1. https://databricks-sdk-py.readthedocs.io/en/latest/workspace/jobs/jobs.html
Databricks con il Python SDK

Sintassi Cron

Espressioni Cron per pianificare:

  1. 03:30:00 ogni giorno = 0 30 3 * * ?
  2. 14:45:00 ogni giorno = 0 45 2 * * ?
1 https://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html
Databricks con il Python SDK

Pianificare un job

Possiamo pianificare un job che esegue il notebook ogni giorno alle 3:00

# Crea un job che esegue Datacamp_Test_Notebook
cron_expression = "0 0 3 * * ?"

created_job = w.jobs.create( name='sdk-dc-project-task', tasks=[jobs.Task(description="test",
notebook_task=jobs.NotebookTask(notebook_path=notebook_path), task_key="my-key")], timeout_seconds=3600,
schedule=jobs.CronSchedule(quartz_cron_expression=cron_expression, timezone_id="America/New_York") )
1 https://databricks-sdk-py.readthedocs.io/en/latest/workspace/jobs/jobs.html
Databricks con il Python SDK

Passons à la pratique !

Databricks con il Python SDK

Preparing Video For Download...