メインコンテンツまでスキップ

Serverlessジョブの環境変数を構成する

環境変数は、Serverless Lakeflow Jobsタスク内のアプリケーションコードに対して、デプロイメント固有の構成を提供します。ジョブ上で名前付き環境変数のエントリを定義し、それを必要とする各タスクに対してエントリを選択します。ベータ期間中は、Jobs APIを通じて環境変数を構成します。

備考

ベータ版

この機能はベータ版です。使用するには、ワークスペース管理者は、 プレビュー ページから Lakeflow Jobsの環境変数 を有効にする必要があります。Databricksのプレビューを管理するを参照してください。

このページでは、Serverless ジョブの環境変数について説明します。Serverless コンピュートはinitスクリプトをサポートしていません。もしワークロードがクラシックコンピュートで動作しているなら、「 環境変数をinitスクリプトで設定・使用」を参照してください。

環境変数の仕組み

Environment variable entries are defined at the job level. Each entry has a unique key, contains inline variables, and works with all serverless task types. An entry key can contain only letters, numbers, hyphens, and underscores. Each inline variable name must start with a letter or underscore and can contain only letters, numbers, and underscores.

Each task selects a single environment variable entry with environment_variables_key and receives only that entry's variables. Entries are never combined, and one entry can't inherit from another. A task that omits environment_variables_key receives no environment variables.

Environment variables are available only to application code that runs in the task process. They aren't available to Spark execution logic.

要件

  • A workspace admin has turned on Environment variables in Lakeflow Jobs from the Previews page. See Manage Databricks previews.
  • Serverless環境バージョン5以降で実行されるタスク。

Limitations

User-defined functions (UDFs) can't read environment variables, because UDFs run in Spark execution logic rather than the task process.

以下の制限が適用されます。

構成

上限

Environment variable entries per job

10

エントリーキー

1–100 characters; must match ^[\w\-_]+$

Inline variables per entry

100

Inline variable name

1–256 characters; must match ^[A-Za-z_][A-Za-z0-9_]*$

インライン変数の値

512 characters

構成

上限

Environment variable entries per job

10

エントリーキー

1–100 characters; must match ^[\w\-_]+$

Inline variables per entry

100

Inline variable name

1–256 characters; must match ^[A-Za-z_][A-Za-z0-9_]*$

インライン変数の値

512 characters

Configure environment variables with the Jobs API

POST /api/2.2/jobs/create リクエストの environment_variables フィールドを使用して、環境変数のエントリを定義します。エントリを使用する各タスクで environment_variables_key を設定します。

次の例では、ノートブックタスクとJARタスクに対して個別の環境変数エントリを定義しています。各タスクはenvironment_variables_keyを使用してエントリを選択します。また、すべてのServerless タスクタイプで同じ方法でエントリを使用できます:

JSON
{
"name": "serverless-environment-variables-example",
"tasks": [
{
"task_key": "process_orders",
"notebook_task": {
"notebook_path": "/Workspace/Shared/process-orders"
},
"environment_key": "default",
"environment_variables_key": "production"
},
{
"task_key": "summarize",
"spark_jar_task": {
"main_class_name": "com.company.main"
},
"environment_key": "summary_env",
"environment_variables_key": "summary_vars"
}
],
"environments": [
{
"environment_key": "default",
"spec": {
"environment_version": "5",
"dependencies": []
}
},
{
"environment_key": "summary_env",
"spec": {
"environment_version": "5",
"java_dependencies": ["/Volumes/company/default/prod/summarize.jar"]
}
}
],
"environment_variables": [
{
"environment_variables_key": "production",
"variables": {
"APP_ENV": "production",
"REGION": "us-west-2"
}
},
{
"environment_variables_key": "summary_vars",
"variables": {
"APP_ENV": "production",
"LOG_LEVEL": "INFO",
"LOG_FILE": "service.log"
}
}
]
}

You can also configure environment variable entries with the Update a job, Overwrite all settings for a job, and Submit a one-time run operations.

アプリケーションコードでの環境変数の読み取り

Application code reads configured variables from the process environment.

Python
import os

app_environment = os.environ["APP_ENV"]
region = os.environ["REGION"]

その他のリソース