From 85842a05c207a68e8566923164aef6618636c1a0 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Thu, 28 Nov 2024 14:32:54 +0100 Subject: [PATCH 1/3] Example of Databricks Apps which manages a job created by the same bundle --- knowledge_base/databricks_app/.gitignore | 2 ++ knowledge_base/databricks_app/README.md | 24 +++++++++++++++ knowledge_base/databricks_app/databricks.yml | 14 +++++++++ .../databricks_app/resources/app_job.job.yml | 16 ++++++++++ .../resources/job_manager.app.yml | 29 +++++++++++++++++++ knowledge_base/databricks_app/src/app/app.py | 25 ++++++++++++++++ .../src/app/templates/index.html | 15 ++++++++++ knowledge_base/databricks_app/src/job/main.py | 3 ++ 8 files changed, 128 insertions(+) create mode 100644 knowledge_base/databricks_app/.gitignore create mode 100644 knowledge_base/databricks_app/README.md create mode 100644 knowledge_base/databricks_app/databricks.yml create mode 100644 knowledge_base/databricks_app/resources/app_job.job.yml create mode 100644 knowledge_base/databricks_app/resources/job_manager.app.yml create mode 100644 knowledge_base/databricks_app/src/app/app.py create mode 100644 knowledge_base/databricks_app/src/app/templates/index.html create mode 100644 knowledge_base/databricks_app/src/job/main.py diff --git a/knowledge_base/databricks_app/.gitignore b/knowledge_base/databricks_app/.gitignore new file mode 100644 index 0000000..de811f1 --- /dev/null +++ b/knowledge_base/databricks_app/.gitignore @@ -0,0 +1,2 @@ + +.databricks diff --git a/knowledge_base/databricks_app/README.md b/knowledge_base/databricks_app/README.md new file mode 100644 index 0000000..bc43b19 --- /dev/null +++ b/knowledge_base/databricks_app/README.md @@ -0,0 +1,24 @@ +# Databricks App for working with Databricks jobs + +This example demonstrates how to define an Databricks App in a Databricks Asset Bundle. + +It includes and deploys an example app and a job managed by DABs to a Databricks workspace. +The app shows current status of the job and lists all existing runs. + +For more information about Databricks Apps, please refer to the [documentation](https://docs.databricks.com/en/dev-tools/databricks-apps/index.html). + +## Prerequisites + +* Databricks CLI v0.236.0 or above + +## Usage + +Modify `databricks.yml`: +* Update the `host` field under `workspace` to the Databricks workspace to deploy to. + +Run `databricks bundle deploy` to deploy the app. + +Run `databricks bundle run job_manager` to start the app and execute app deployment. +When you change app code or config, you need to run `databricks bundle deploy` and `databricks bundle run job_manager` to update the app source code and configuration. + +Run `databricks bundle open` to navigate to the deployed app in your browser. Alternatively, run `databricks bundle summary` to display its URL. \ No newline at end of file diff --git a/knowledge_base/databricks_app/databricks.yml b/knowledge_base/databricks_app/databricks.yml new file mode 100644 index 0000000..771c247 --- /dev/null +++ b/knowledge_base/databricks_app/databricks.yml @@ -0,0 +1,14 @@ +bundle: + name: databricks_app + +include: + - resources/*.yml + + +#workspace: +# host: https://myworkspace.databricks.com + +targets: + dev: + default: true + mode: development diff --git a/knowledge_base/databricks_app/resources/app_job.job.yml b/knowledge_base/databricks_app/resources/app_job.job.yml new file mode 100644 index 0000000..6d02e37 --- /dev/null +++ b/knowledge_base/databricks_app/resources/app_job.job.yml @@ -0,0 +1,16 @@ +resources: + jobs: + app_job: + name: Serverless job + tasks: + - task_key: task + spark_python_task: + python_file: ../src/job/main.py + environment_key: default + + environments: + - environment_key: default + spec: + client: "1" + dependencies: + - cowsay \ No newline at end of file diff --git a/knowledge_base/databricks_app/resources/job_manager.app.yml b/knowledge_base/databricks_app/resources/job_manager.app.yml new file mode 100644 index 0000000..66a29ad --- /dev/null +++ b/knowledge_base/databricks_app/resources/job_manager.app.yml @@ -0,0 +1,29 @@ +resources: + apps: + job_manager: + name: "job_manager" + description: "App which manages job created by this bundle" + + # The location of the source code for the app. + source_code_path: ../src/app + + # The configuration for the app. This allows to pass environment variables to the app and + # configure how to run the app. + config: + command: + - flask + - --app + - app + - run + - --debug + env: + - name: JOB_ID + valueFrom: "app-job" + + # The resources which this app have an access to. + resources: + - name: "app-job" + description: "A job for app to be able to work with" + job: + id: ${resources.jobs.app_job.id} + permission: "CAN_MANAGE_RUN" diff --git a/knowledge_base/databricks_app/src/app/app.py b/knowledge_base/databricks_app/src/app/app.py new file mode 100644 index 0000000..4cfc3d3 --- /dev/null +++ b/knowledge_base/databricks_app/src/app/app.py @@ -0,0 +1,25 @@ +import os + +from databricks.sdk import WorkspaceClient +from flask import Flask, render_template, redirect, url_for + +app = Flask(__name__) + + +@app.route("/") +def home(): + job_id = os.getenv("JOB_ID") + + w = WorkspaceClient() + job = w.jobs.get(job_id) + runs = w.jobs.list_runs(job_id=job_id) + return render_template("index.html", job_name=job.settings.name, runs=runs) + + +@app.route("/run") +def run_job(): + job_id = os.getenv("JOB_ID") + + w = WorkspaceClient() + w.jobs.run_now(job_id=job_id) + return redirect(url_for("home"), code=302) diff --git a/knowledge_base/databricks_app/src/app/templates/index.html b/knowledge_base/databricks_app/src/app/templates/index.html new file mode 100644 index 0000000..815c555 --- /dev/null +++ b/knowledge_base/databricks_app/src/app/templates/index.html @@ -0,0 +1,15 @@ + + + Databricks App managed by DABs + + +

This app manages job "{{job_name}}"

+

Job runs

+ + Run job + + \ No newline at end of file diff --git a/knowledge_base/databricks_app/src/job/main.py b/knowledge_base/databricks_app/src/job/main.py new file mode 100644 index 0000000..753eb3d --- /dev/null +++ b/knowledge_base/databricks_app/src/job/main.py @@ -0,0 +1,3 @@ +from cowsay import cow + +cow("Hello, world!") From 6aca06aaac833f4b4cef217346cda838577a425d Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Tue, 24 Dec 2024 13:24:12 +0100 Subject: [PATCH 2/3] fixes --- .../resources/{app_job.job.yml => hello_world.job.yml} | 4 ++-- knowledge_base/databricks_app/resources/job_manager.app.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename knowledge_base/databricks_app/resources/{app_job.job.yml => hello_world.job.yml} (84%) diff --git a/knowledge_base/databricks_app/resources/app_job.job.yml b/knowledge_base/databricks_app/resources/hello_world.job.yml similarity index 84% rename from knowledge_base/databricks_app/resources/app_job.job.yml rename to knowledge_base/databricks_app/resources/hello_world.job.yml index 6d02e37..189ea0c 100644 --- a/knowledge_base/databricks_app/resources/app_job.job.yml +++ b/knowledge_base/databricks_app/resources/hello_world.job.yml @@ -1,7 +1,7 @@ resources: jobs: - app_job: - name: Serverless job + hello_world: + name: Serverless Hello World job tasks: - task_key: task spark_python_task: diff --git a/knowledge_base/databricks_app/resources/job_manager.app.yml b/knowledge_base/databricks_app/resources/job_manager.app.yml index 66a29ad..14b7e64 100644 --- a/knowledge_base/databricks_app/resources/job_manager.app.yml +++ b/knowledge_base/databricks_app/resources/job_manager.app.yml @@ -18,12 +18,12 @@ resources: - --debug env: - name: JOB_ID - valueFrom: "app-job" + value: ${resources.jobs.hello_world.id} # The resources which this app have an access to. resources: - name: "app-job" description: "A job for app to be able to work with" job: - id: ${resources.jobs.app_job.id} + id: ${resources.jobs.hello_world.id} permission: "CAN_MANAGE_RUN" From 62f9ea1220bddd235995bec32b5107613e1b0000 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Tue, 24 Dec 2024 13:27:13 +0100 Subject: [PATCH 3/3] updated required cli version --- knowledge_base/databricks_app/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knowledge_base/databricks_app/README.md b/knowledge_base/databricks_app/README.md index bc43b19..b524352 100644 --- a/knowledge_base/databricks_app/README.md +++ b/knowledge_base/databricks_app/README.md @@ -9,7 +9,7 @@ For more information about Databricks Apps, please refer to the [documentation]( ## Prerequisites -* Databricks CLI v0.236.0 or above +* Databricks CLI v0.238.0 or above ## Usage