Skip to the page

Databricks interview question · hard

Lois Lane needs to schedule a Databricks notebook to run daily and send an email notification upon completion. How can this be achieved using Databricks Jobs, and how can she configure email alerts to send to the Daily Planet?

The short answerLois can achieve this using Databricks Jobs and configuring email alerts.

The full answer

Lois can achieve this using Databricks Jobs and configuring email alerts. Here's a breakdown:

1. Creating a Databricks Job:

  • Navigate to Jobs: In the Databricks workspace, click on the "Workflows" tab in the sidebar, then select "Jobs".
  • Create a new job: Click the "Create Job" button.
  • Configure the job:
    • Task: Choose "Notebook" as the task type.
    • Source: Select the notebook Lois wants to schedule.
    • Cluster: Select an existing cluster or create a new job cluster. Job clusters are recommended for scheduled jobs as they automatically terminate after completion, saving costs.
    • Schedule: Set the schedule to "Daily" and specify the desired start time.
    • Alerts: This is where Lois will configure email notifications.

2. Configuring Email Alerts:

Within the job configuration's "Alerts" section, Lois can set up email notifications:

  • On start: Receive an email when the job starts.
  • On success: Receive an email when the job completes successfully.
  • On failure: Receive an email when the job fails.
  • On timeout: Receive an email if the job exceeds the specified timeout duration.

For each of these events, Lois can specify:

  • Alert destinations: Add email addresses (e.g., dailyplanet@example.com) or groups to receive the notifications.

3. Using Job Clusters (Recommended):

For scheduled jobs, it's highly recommended to use job clusters. These clusters are automatically created when the job starts and terminated when it finishes. This avoids the cost of running an always-on cluster. When configuring the job, Lois should select "New Job Cluster" and specify the cluster configuration (worker type, number of workers, etc.).

4. Using the Databricks REST API (For Automation):

For more advanced scenarios or integration with external systems, Lois can use the Databricks REST API to create and manage jobs, including setting up email alerts.

Example using the Databricks CLI (which uses the REST API):

databricks jobs create   --name "Daily Report Job"   --notebook-task notebook_path="/path/to/your/notebook"   --new-cluster '{"spark_version": "13.x-scala2.12", "node_type_id": "i3.xlarge", "num_workers": 2}'   --schedule '{"quartz_cron_expression": "0 0 0 * * ?", "timezone": "UTC"}'   --email-notifications '{"on_success": ["dailyplanet@example.com"], "on_failure": ["dailyplanet@example.com"]}'

This command creates a job that runs daily at midnight UTC, using a new cluster with two i3.xlarge workers, and sends email notifications on success or failure.

Key Considerations:

  • Email Configuration: Ensure that email notifications are enabled in the Databricks workspace settings.
  • Alert Group: Create a distribution list or group for the Daily Planet to simplify managing email recipients.
  • Error Handling: Implement robust error handling within the notebook to provide informative error messages in the email notifications in case of failures.
  • Monitoring: Regularly monitor the job execution and email notifications to ensure the job is running as expected.

By following these steps, Lois can effectively schedule her notebook to run daily and keep the Daily Planet informed about the results through email notifications.

Last updated 17 January 2025