databricks-serverless

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Deploying Databricks jobs and pipelines on serverless

在 Serverless 上部署 Databricks 作业和管道

FIRST: Use the parent
databricks-core
skill for CLI basics, authentication and profile selection.
This skill decides compute. Where another skill or example (for example
databricks-jobs
) shows cluster configuration, follow this skill instead.
Serverless is the default compute for every job, pipeline and bundle you create. If the user explicitly asks for a cluster, do what they ask and mention the serverless option in one line. When editing an existing job, keep its compute unless the user asks to change it; tasks you add follow this skill.
首先:使用父级
databricks-core
skill 了解 CLI 基础、身份验证和 profile 选择。
此 skill 负责决定计算资源。当其他 skill 或示例(例如
databricks-jobs
)显示集群配置时,应遵循此 skill。
Serverless 是你创建的每个作业、管道和 bundle 的默认计算资源。如果用户明确要求使用集群,则照做,并在一行内提及 Serverless 选项。编辑现有作业时,保留其计算资源,除非用户要求更改;新增任务遵循此 skill。

1. Serverless or classic

1. Serverless 还是 classic

Use classic compute only for:
  • R code, or Scala in notebook cells (
    %scala
    ): serverless notebooks run Python and SQL only;
  • custom Docker images;
  • OS packages (
    apt-get
    ) or native libraries with no pip equivalent;
  • custom Spark data source JARs;
  • a stated hard requirement for an instance type or Databricks Runtime version;
  • an explicit user request;
  • existing code over the budget in section 5;
  • a workspace without serverless: if creating or running the job fails because serverless is not available there, put the whole job on classic and say so.
Then give only the blocked task a cluster (one small
job_clusters
entry with the latest LTS runtime and
autoscale: {min_workers: 1, max_workers: 4}
), keep every other task serverless, and state the blocker in one line. For a blocked pipeline, give the pipeline a
clusters
block instead of
serverless: true
.
Not reasons for classic: Scala or Java in a JAR task (JAR tasks run on serverless), GPUs (serverless GPU), large data, long runtimes, Kafka, cost (serverless STANDARD is on par with or cheaper than on-demand classic in most cases), or habit.
仅在以下情况下使用 classic 计算资源:
  • R 代码,或 notebook 单元格中的 Scala(
    %scala
    ):Serverless notebook 仅运行 Python 和 SQL;
  • 自定义 Docker 镜像;
  • 操作系统包(
    apt-get
    )或没有 pip 等价物的 native library;
  • 自定义 Spark data source JAR;
  • 明确要求使用特定 instance type 或 Databricks Runtime 版本;
  • 用户明确要求使用 classic;
  • 现有代码超出第 5 节中的预算;
  • 工作区不支持 Serverless:如果创建或运行作业时因该工作区无法使用 Serverless 而失败,则将整个作业放在 classic 上,并明确说明。
然后,只为受阻任务配置集群(一个小型
job_clusters
条目,使用最新 LTS runtime 和
autoscale: {min_workers: 1, max_workers: 4}
),让其他所有任务继续使用 Serverless,并在一行内说明阻碍原因。对于受阻管道,配置
clusters
块,而不是
serverless: true
。
以下情况不是使用 classic 的理由:JAR 任务中的 Scala 或 Java(JAR 任务可运行在 Serverless 上)、GPU(可使用 Serverless GPU)、大数据量、长时间运行、Kafka、成本(在大多数情况下,Serverless STANDARD 的成本与按需 classic 相当或更低),以及习惯性选择 classic。

2. Put every task on serverless

2. 将所有任务放在 Serverless 上

  • New jobs: leave out
    new_cluster
    ,
    job_clusters
    ,
    job_cluster_key
    ,
    existing_cluster_id
    ,
    instance_pool_id
    ,
    node_type_id
    ,
    num_workers
    ,
    autoscale
    ,
    spark_version
    . A task without a cluster runs on serverless. In an existing job, keep the cluster settings it has.
  • Notebook tasks need nothing else. Python script, wheel and JAR tasks take their libraries from a job-level environment. Use the latest environment version the workspace offers, as a quoted string:
    yaml
    environments:
      - environment_key: default
        spec:
          environment_version: "4"                # example; use the latest
          dependencies: ["requests==2.32.3"]   # pin versions; JARs go in java_dependencies
    tasks:
      - task_key: main
        spark_python_task: {python_file: /Workspace/.../main.py}
        environment_key: default
  • SQL tasks: a serverless SQL warehouse. Pipelines:
    serverless: true
    , no
    clusters
    block.
  • Streaming:
    .trigger(availableNow=True)
    , the trigger serverless supports (no
    processingTime
    or
    continuous
    triggers). For always-on processing, run that stream in a continuous job (
    continuous: {pause_status: UNPAUSED}
    ), which starts the next run as soon as one finishes, or use a continuous pipeline.
  • Tag every job you create for resource tracking, as other Databricks skills do: job-level
    tags: {"aidevkit_project": "ai-dev-kit"}
    . Keep tags the user already has.
  • 新作业: 省略
    new_cluster
    、
    job_clusters
    、
    job_cluster_key
    、
    existing_cluster_id
    、
    instance_pool_id
    、
    node_type_id
    、
    num_workers
    、
    autoscale
    和
    spark_version
    。未配置集群的任务将在 Serverless 上运行。对于现有作业,保留其已有的集群设置。
  • Notebook 任务无需其他配置。Python 脚本、wheel 和 JAR 任务从作业级 environment 获取依赖库。使用 workspace 提供的最新 environment 版本,并将其写成带引号的字符串:
    yaml
    environments:
      - environment_key: default
        spec:
          environment_version: "4"                # example; use the latest
          dependencies: ["requests==2.32.3"]   # pin versions; JARs go in java_dependencies
    tasks:
      - task_key: main
        spark_python_task: {python_file: /Workspace/.../main.py}
        environment_key: default
  • SQL 任务: 使用 Serverless SQL warehouse。管道: 使用
    serverless: true
    ,不要配置
    clusters
    块。
  • 流处理: 使用
    .trigger(availableNow=True)
    ,这是 Serverless 支持的触发方式(不支持
    processingTime
    或
    continuous
    trigger)。如需持续运行,请使用 continuous job 运行该流(
    continuous: {pause_status: UNPAUSED}
    ),这样上一轮结束后会立即启动下一轮;也可以使用 continuous pipeline。
  • 按照其他 Databricks skill 的做法,为创建的每个作业添加标签以进行资源跟踪:作业级
    tags: {"aidevkit_project": "ai-dev-kit"}
    。保留用户已有的标签。

3. Choose the performance mode

3. 选择性能模式

Set the job-level
performance_target
:
  • STANDARD
    when nothing is time-critical and cost matters more than speed: scheduled batch and ETL, nightly or weekly runs, or any job where the user states no latency need. Runs start within minutes instead of seconds and cost less. This is the default choice.
  • PERFORMANCE_OPTIMIZED
    only when the user names an SLA or a deadline that is tight relative to the runtime, a person waits on the result, or the job runs every 30 minutes or more often.
performance_target
applies to the whole job, notebook tasks included (only interactive notebooks always run performance-optimized). Say which mode you chose and why in one line, e.g. "STANDARD: nightly batch, no deadline." Serverless GPU tasks ignore
performance_target
.
Pipelines have no
performance_target
of their own. To schedule a pipeline, trigger it from a job with a
pipeline_task
and set the mode on that job; the job's mode applies to the pipeline update. A continuous pipeline runs in STANDARD only when a continuous job runs it.
设置作业级
performance_target
:
  • 当任务没有时间敏感性且成本比速度更重要时,使用
    STANDARD
    :包括定时批处理和 ETL、每夜或每周运行,以及用户明确表示没有延迟要求的任何作业。作业会在数分钟内启动,而不是数秒,并且成本更低。这是默认选择。
  • 仅当用户指定了相对于运行时间较为紧迫的 SLA 或截止时间、有人正在等待结果,或作业每 30 分钟或更频繁地运行时,才使用
    PERFORMANCE_OPTIMIZED
    。
performance_target
适用于整个作业,包括 notebook 任务(只有交互式 notebook 始终以 performance-optimized 模式运行)。用一行说明选择的模式及原因,例如:“STANDARD:夜间批处理,无截止时间。”
Serverless GPU 任务会忽略
performance_target
。
管道本身没有
performance_target
。要调度管道,请使用包含
pipeline_task
的作业触发管道,并在该作业上设置模式;作业的模式将应用于管道更新。只有当 continuous job 运行 continuous pipeline 时,管道才以 STANDARD 模式运行。

4. Code you write runs on serverless from the start

4. 从一开始,你编写的代码就在 Serverless 上运行

Serverless reads Unity Catalog tables,
/Volumes/...
paths, cloud paths behind a Unity Catalog external location, and the sample data under
dbfs:/databricks-datasets/
. It cannot read DBFS mounts (
dbfs:/mnt/...
) or other DBFS root paths; only those count as a DBFS blocker.
DataFrame and SQL APIs only (no RDDs,
sc.*
,
spark.sparkContext
); Unity Catalog three-part table names;
/Volumes/...
paths instead of DBFS mounts (
dbfs:/mnt/...
);
availableNow
streaming triggers; no
.cache()
or
.persist()
; no
spark.conf.set
for executor, driver or memory settings; pinned dependencies in the environment, never init scripts.
Serverless 可以读取 Unity Catalog 表、
/Volumes/...
路径、Unity Catalog external location 后面的云路径,以及
dbfs:/databricks-datasets/
下的示例数据。它无法读取 DBFS mount(
dbfs:/mnt/...
)或其他 DBFS root path;只有这些情况才构成 DBFS blocker。
仅使用 DataFrame 和 SQL API(不得使用 RDD、
sc.*
、
spark.sparkContext
);使用 Unity Catalog 三段式表名;使用
/Volumes/...
路径而不是 DBFS mount(
dbfs:/mnt/...
);使用
availableNow
streaming trigger;不得使用
.cache()
或
.persist()
;不得使用
spark.conf.set
设置 executor、driver 或内存;在 environment 中固定依赖版本,绝不使用 init script。

5. Existing code: a quick check, not a migration

5. 现有代码:快速检查,而非迁移

The user is waiting for the deployment. Spend one short pass, then deploy.
  1. Scan only the files the job runs (a text search such as
    grep
    or
    rg
    ; do not read the whole repo) for:
    sc.
    ,
    sparkContext
    ,
    .rdd
    ,
    parallelize
    ,
    mapPartitions
    ,
    reduceByKey
    ,
    groupByKey
    ,
    dbfs:/
    ,
    /dbfs/
    ,
    dbutils.fs.mount
    ,
    .cache(
    ,
    .persist(
    ,
    spark.conf.set
    ,
    processingTime
    ,
    continuous=
    ,
    writeStream
    ,
    hive_metastore.
    ,
    GLOBAL TEMP
    ,
    hivevar
    ,
    init_scripts
    ,
    apt-get
    ,
    docker
    , and other languages:
    %scala
    or
    %r
    as the whole magic (
    # MAGIC %r
    , not
    %run
    ),
    .r
    files and R notebooks,
    SparkR
    and
    sparklyr
    . R and Scala cells go to classic (section 1).
  2. Minor blockers: fix them and deploy on serverless if every fix is mechanical and local, keeps the logic, needs no new infrastructure, and there are about 5 small edits or fewer in total:
    • delete
      .cache()
      ,
      .persist()
      ,
      .unpersist()
      ,
      setLogLevel
      , executor/driver/AQE
      spark.conf.set
      lines
    • move
      pip install
      s or
      requirements.txt
      into the environment, pinned
    • add
      .trigger(availableNow=True)
      to a stream that sets no trigger, in a job that runs on a schedule anyway (a stream with a
      processingTime
      or
      continuous
      trigger is a step 4 blocker)
    • replace a
      dbfs:/mnt/...
      path with a
      /Volumes/...
      path only when that volume's storage location is the mount's source, so the job reads the same data; a volume with a similar name is not enough. Otherwise the mount is a step 4 blocker.
    • CREATE GLOBAL TEMP VIEW
      becomes
      CREATE TEMP VIEW
      , and its
      global_temp.<view>
      reads become
      <view>
      , when the same file creates and reads the view (serverless has no global temp views); a global temp view that another task or notebook reads is a step 4 blocker
    • add the catalog to a two-part table name only when you know which Unity Catalog catalog holds the table (for example the job's default catalog); a table in
      hive_metastore
      is a step 4 blocker
    List every edit you made in your reply.
  3. RDD or SparkContext code: translate it to DataFrames and deploy on serverless only if all of these hold:
    • it is one file of about 200 lines or fewer;
    • the change is a pure API translation: the same logic, expressed with DataFrame or SQL;
    • every input path, table, output table and parameter stays exactly as it was. Never swap in another dataset, even a sample one;
    • every input is already readable on serverless (section 4: a Unity Catalog table, an existing
      /Volumes/...
      path, a cloud path behind an external location, or
      dbfs:/databricks-datasets/
      ), so nothing new has to be set up.
    Save the original next to it as
    <name>.classic.py
    and point the job at the translated file. Tell the user that the translation has not been run yet and suggest one test run that compares the output with the classic version.
  4. Anything bigger: do not rewrite. Put that task on classic now (section 1), keep the other tasks serverless, and end with: "Kept <task> on classic because <blocker>. To move it to serverless, use the databricks-serverless-migration skill." Bigger means: RDD code that fails any condition in step 3 (for example it reads a DBFS mount), mounts with no volume on the same storage, Hive Metastore to Unity Catalog, recompiling a JAR, networking changes, changing what a stream does (an always-on
    processingTime
    or
    continuous
    stream), or more than about 5 edits besides a step 3 translation.
Do not stop to ask whether to migrate. Deploy, then report what you did.
用户正在等待部署。花一次简短检查的时间,然后完成部署。
  1. 仅扫描作业实际运行的文件(使用
    grep
    或
    rg
    等文本搜索;不要读取整个 repo),查找:
    sc.
    、
    sparkContext
    、
    .rdd
    、
    parallelize
    、
    mapPartitions
    、
    reduceByKey
    、
    groupByKey
    、
    dbfs:/
    、
    /dbfs/
    、
    dbutils.fs.mount
    、
    .cache(
    、
    .persist(
    、
    spark.conf.set
    、
    processingTime
    、
    continuous=
    、
    writeStream
    、
    hive_metastore.
    、
    GLOBAL TEMP
    、
    hivevar
    、
    init_scripts
    、
    apt-get
    、
    docker
    ,以及其他语言的使用方式:将
    %scala
    或
    %r
    作为完整的 magic(
    # MAGIC %r
    ,而不是
    %run
    )、
    .r
    文件和 R notebook、
    SparkR
    和
    sparklyr
    。R 和 Scala 单元格应转到 classic(第 1 节)。
  2. 轻微阻碍:如果每项修复都只是机械式且局部的修改、不改变逻辑、不需要新基础设施,并且总共约不超过 5 处小改动,则修复后部署到 Serverless:
    • 删除
      .cache()
      、
      .persist()
      、
      .unpersist()
      、
      setLogLevel
      ,以及 executor、driver 和 AQE 的
      spark.conf.set
      配置行
    • 将
      pip install
      命令或
      requirements.txt
      移入 environment,并固定依赖版本
    • 对于按计划运行的作业,如果某个流未设置 trigger,则添加
      .trigger(availableNow=True)
      (使用
      processingTime
      或
      continuous
      trigger 的流属于第 4 步的 blocker)
    • 仅当某个 volume 的存储位置与 mount 的源存储位置相同时,才将
      dbfs:/mnt/...
      路径替换为
      /Volumes/...
      路径,以确保作业读取相同的数据;仅名称相似的 volume 不足以满足条件。否则,该 mount 属于第 4 步的 blocker
    • 当同一个文件负责创建和读取 view 时,将
      CREATE GLOBAL TEMP VIEW
      改为
      CREATE TEMP VIEW
      ,并将
      global_temp.<view>
      读取改为
      <view>
      (Serverless 不支持 global temp view);如果其他任务或 notebook 会读取该 global temp view,则它属于第 4 步的 blocker
    • 仅当明确知道哪个 Unity Catalog catalog 包含目标表时,才为两段式表名添加 catalog;例如,可以使用作业的默认 catalog。位于
      hive_metastore
      中的表属于第 4 步的 blocker
    在回复中列出所做的每项修改。
  3. RDD 或 SparkContext 代码:仅在满足以下所有条件时,才将其转换为 DataFrame 并部署到 Serverless:
    • 只有一个文件,长度约不超过 200 行;
    • 转换仅涉及 API,将相同逻辑改写为 DataFrame 或 SQL;
    • 每个输入路径、表、输出表和参数都保持原样。绝不替换成其他数据集,即使使用示例数据集也不允许;
    • 每个输入都已可在 Serverless 上读取(第 4 节:Unity Catalog 表、现有的
      /Volumes/...
      路径、external location 后面的云路径,或
      dbfs:/databricks-datasets/
      ),因此不需要设置任何新基础设施。
    将原文件保存在转换后的文件旁边,并命名为
    <name>.classic.py
    ,然后让作业指向转换后的文件。 告知用户该转换尚未运行,并建议进行一次测试运行,将输出与 classic 版本进行比较。
  4. 任何规模更大的改动:不要重写。 立即将该任务放在 classic 上(第 1 节),让其他任务继续使用 Serverless,并以这句话结束:“已将 <task> 保留在 classic 上,因为 <blocker>。要将其迁移到 Serverless,请使用 databricks-serverless-migration skill。” 较大改动包括:不符合第 3 步任一条件的 RDD 代码(例如读取 DBFS mount)、同一存储上没有对应 volume 的 mount、从 Hive Metastore 迁移到 Unity Catalog、重新编译 JAR、网络变更、更改流的处理方式(始终运行的
    processingTime
    或
    continuous
    流),或者除第 3 步转换外还有约 5 处以上的改动。
不要停下来询问是否迁移。完成部署,然后报告所做的工作。

Common Issues

常见问题

IssueSolution
Libraries field is not supported for serverless task
Move libraries into
environments[].spec.dependencies
(JARs:
java_dependencies
) and set
environment_key
on the task
Serverless job starts too slowly
performance_target: PERFORMANCE_OPTIMIZED
, only if the user needs fast starts (section 3)
Only remote Spark sessions ... are supported
or another RDD error
RDD/SparkContext code: section 5, step 3 or 4
[PATH_NOT_FOUND]
or no access on
dbfs:/mnt/...
DBFS mounts do not work on serverless: use a volume on the same storage (section 5, step 2), otherwise step 4
Serverless is not enabled or not available in the workspacePut the job on classic and tell the user (section 1)
A sibling skill's example uses
job_clusters
for a new job
Leave the cluster block out; this skill decides compute. Existing jobs keep their compute
问题解决方案
Libraries field is not supported for serverless task
将依赖库移入
environments[].spec.dependencies
(JAR 使用
java_dependencies
),并在任务上设置
environment_key
Serverless 作业启动过慢使用
performance_target: PERFORMANCE_OPTIMIZED
,但仅在用户需要快速启动时使用(第 3 节)
Only remote Spark sessions ... are supported
或其他 RDD 错误
RDD/SparkContext 代码:第 5 节第 3 步或第 4 步
[PATH_NOT_FOUND]
或无法访问
dbfs:/mnt/...
DBFS mount 不支持 Serverless:使用同一存储上的 volume(第 5 节第 2 步),否则按第 4 步处理
工作区未启用或无法使用 Serverless将作业放在 classic 上并告知用户(第 1 节)
兄弟 skill 的示例为新作业使用
job_clusters
省略集群块;由本 skill 决定计算资源。现有作业保留其计算资源

Related Skills

相关 Skill

  • databricks-core - CLI basics, authentication, profiles
  • databricks-jobs - job structure, task types, triggers, notifications
  • databricks-pipelines - Lakeflow Spark Declarative Pipelines
  • databricks-serverless-migration - migrating existing classic workloads to serverless
  • databricks-core - CLI 基础、身份验证、profile
  • databricks-jobs - 作业结构、任务类型、trigger、通知
  • databricks-pipelines - Lakeflow Spark Declarative Pipelines
  • databricks-serverless-migration - 将现有 classic 工作负载迁移到 Serverless

Documentation

文档