To identify the run_id of the last run of a specific Databricks job (or workflow), you can use the Databricks REST API's GET /api/2.0/jobs/runs/list endpoint. This endpoint returns a list of runs for a specified job, sorted in descending order by their start time. By examining the most recent entry, you can obtain the run_id of the last run.


Here’s how you can catch the run_id of the most recent run for a given job:

 

 

import requests

# Replace with your Databricks workspace URL and access token
workspace_url = 'https://<databricks-instance>'
access_token = 'your_access_token'

# Replace with your job ID
job_id = '<job_id>'

# Set up the request headers with the access token
headers = {
    'Authorization': f'Bearer {access_token}'
}

# Make the API request to list runs for the specified job
response = requests.get(f'{workspace_url}/api/2.0/jobs/runs/list?job_id={job_id}', headers=headers)

# Check if the request was successful
if response.status_code == 200:
    runs = response.json().get('runs', [])
    if runs:
        # Get the most recent run (first in the list)
        last_run = runs[0]
        last_run_id = last_run.get('run_id')
        print(f'Last Run ID: {last_run_id}')
    else:
        print('No runs found for the specified job.')
else:
    print(f'Error: {response.status_code} - {response.text}')

 


 

View solution in original post