Membuat templat bundel kustom

Dalam tutorial ini, Anda akan membuat templat kustom untuk membuat Bundel Automasi Deklaratif yang menjalankan pekerjaan dengan tugas Python pada kluster menggunakan gambar kontainer Docker tertentu.

Untuk informasi tentang templat bundel kustom, lihat templat bundel kustom .

Persyaratan

  • Instal Databricks CLI versi 0.218.0 atau lebih tinggi. Jika Anda sudah menginstalnya, konfirmasikan versinya adalah 0.218.0 atau lebih tinggi dengan menjalankan databricks -version dari baris perintah.

Menentukan variabel permintaan pengguna

Pertama, tentukan databricks bundle init variabel permintaan pengguna. Dari baris perintah:

  1. Buat folder kosong bernama dab-container-template:

    mkdir dab-container-template
    
  2. Di akar folder, buat file bernama databricks_template_schema.json:

    cd dab-container-template
    touch databricks_template_schema.json
    
  3. Tambahkan JSON berikut ke databricks_template_schema.json file untuk menentukan permintaan pengguna untuk nama proyek bundel:

    {
      "properties": {
        "project_name": {
          "type": "string",
          "default": "project_name",
          "description": "Project name",
          "order": 1
        }
      }
    }
    

Buat struktur folder bundel

Selanjutnya, buat template folder untuk berisi struktur folder untuk bundel yang Anda hasilkan. Nama subdirektori dan file mengikuti sintaks templat paket Go.

Templat ini membuat folder proyek bundel berdasarkan perintah nama proyek:

mkdir -p "template/{{.project_name}}"

Sekarang buat subdirektori resources dan src untuk file bundel:

mkdir -p "template/{{.project_name}}/resources"
mkdir -p "template/{{.project_name}}/src"

Menambahkan templat konfigurasi YAML

template/{{.project_name}} Di folder , buat file bernama databricks.yml.tmpl:

touch template/{{.project_name}}/databricks.yml.tmpl

Tambahkan YAML berikut ke databricks.yml.tmpl. Contoh ini menggunakan pembantu templat bundel.

# This is a bundle definition for {{.project_name}}.
# See https://docs.databricks.com/dev-tools/bundles/index.html for documentation.
bundle:
  name: {{.project_name}}

include:
  - resources/*.yml

targets:
  # The 'dev' target, used for development purposes.
  # Whenever a developer deploys using 'dev', they get their own copy.
  dev:
    # We use 'mode: development' to make sure everything deployed to this target gets a prefix
    # like '[dev my_user_name]'. Setting this mode also disables any schedules and
    # automatic triggers for jobs and enables the 'development' mode for :re[LDP].
    mode: development
    default: true
    workspace:
      host: {{workspace_host}}

  # The 'prod' target, used for production deployment.
  prod:
    # For production deployments, we only have a single copy, so we override the
    # workspace.root_path default of
    # /Workspace/Users/${workspace.current_user.userName}/.bundle/${bundle.target}/${bundle.name}
    # to a path that is not specific to the current user.
    #
    # By making use of 'mode: production' we enable strict checks
    # to make sure we have correctly configured this target.
    mode: production
    workspace:
      host: {{workspace_host}}
      root_path: /Shared/.bundle/prod/${bundle.name}
    {{- if not is_service_principal}}
    run_as:
      # This runs as {{user_name}} in production. Alternatively,
      # a service principal could be used here using service_principal_name
      # (see Databricks documentation).
      user_name: {{user_name}}
    {{end -}}

Buat file YAML lain bernama {{.project_name}}_job.yml.tmpl di template/{{.project_name}}/resources folder . File YAML baru ini berisi definisi pekerjaan.

touch template/{{.project_name}}/resources/{{.project_name}}_job.yml.tmpl

Tambahkan YAML berikut ke file ini untuk menjelaskan pekerjaan templat, yang berisi tugas Python untuk dijalankan pada kluster pekerjaan menggunakan gambar kontainer Docker tertentu. Contoh ini menggunakan gambar kontainer Docker dasar Databricks default, tetapi Anda dapat menentukan gambar kustom Anda sendiri sebagai gantinya.

# The main job for {{.project_name}}
resources:
  jobs:
    {{.project_name}}_job:
      name: {{.project_name}}_job
      tasks:
        - task_key: python_task
          job_cluster_key: job_cluster
          spark_python_task:
            python_file: ../src/task.py

      job_clusters:
        - job_cluster_key: job_cluster
          new_cluster:
            docker_image:
              url: databricksruntime/python:10.4-LTS
            node_type_id: i3.xlarge
            spark_version: 13.3.x-scala2.12

Menambahkan file yang dirujuk dalam konfigurasi Anda

Selanjutnya, buat file tugas Python yang dirujuk oleh pekerjaan dalam templat:

touch template/{{.project_name}}/src/task.py

Sekarang, tambahkan yang berikut ini ke task.py:

print(f'Spark version{spark.version}')

Memverifikasi struktur templat bundel

Tinjau struktur folder proyek templat bundel Anda. Ini akan terlihat seperti ini:

dab-container-template
├── databricks_template_schema.json
└── template
    ├── {{.project_name}}
        ├── databricks.yml.tmpl
        ├── resources
        │   └── {{.project_name}}_job.yml.tmpl
        └── src
            └── task.py

Menguji templat Anda

Terakhir, uji templat bundel Anda. Untuk menghasilkan bundel berdasarkan templat kustom baru Anda, gunakan perintah databricks bundle init, menentukan lokasi templat baru. Dari folder akar proyek bundel Anda:

databricks bundle init dab-container-template

Langkah berikutnya

Sumber daya