Databricks Connect 的計算組態

注意

本文涵蓋 Databricks Runtime 13.3 LTS 及以上版本的 Databricks Connect。

本頁介紹了不同方式以配置 Databricks Connect 與您的 Azure Databricks 群集無伺服器計算之間的連線。

Databricks Connect 讓你能將熱門的 IDE,如 Visual Studio Code、PyCharm、RStudio Desktop、IntelliJ IDEA、筆記型電腦伺服器及其他自訂應用程式,連接到 Azure Databricks 叢集。 請參閱 Databricks Connect

設定

在開始之前,您需要下列項目:

設定叢集的連線

有多種方式可設定叢集的連線。 Databricks Connect 會依下列順序搜尋組態屬性,並使用它找到的第一個組態。 如需進階設定資訊,請參閱 Databricks Connect 的進階使用方式

  1. DatabricksSession 類別的 remote() 方法
  2. Databricks 組態配置檔
  3. DATABRICKS_CONFIG_PROFILE環境變數
  4. 每個組態屬性的環境變數
  5. 名為 DEFAULT 的 Databricks 組態配置檔

類別 DatabricksSessionremote() 方法

此選項僅適用於 Authenticate with Azure Databricks Personal Access Tokens(舊版),請指定工作區實例名稱、Azure Databricks個人存取權杖及叢集 ID。

您可以透過數種方式初始化 類別 DatabricksSession

  • host中設定 tokencluster_idDatabricksSession.builder.remote() 字段。
  • 使用 Databricks SDK 的 Config 類別。
  • 指定 Databricks 設定檔連同 cluster_id 欄位。

Databricks 建議透過環境變數或組態檔設定屬性,而不是在您的程式代碼中指定這些連接屬性,如本節所述。 以下程式碼範例假設你提供了某種實作來實現所提議的retrieve_*函式,以取得使用者或其他組態儲存庫(如 Azure KeyVault)的必要屬性。

下列每個方法的程式代碼如下:

Python

# Set the host, token, and cluster_id fields in DatabricksSession.builder.remote.
# If you have already set the DATABRICKS_CLUSTER_ID environment variable with the
# cluster's ID, you do not also need to set the cluster_id field here.
from databricks.connect import DatabricksSession

spark = DatabricksSession.builder.remote(
host       = f"https://{retrieve_workspace_instance_name()}",
token      = retrieve_token(),
cluster_id = retrieve_cluster_id()
).getOrCreate()

程式語言 Scala

// Set the host, token, and clusterId fields in DatabricksSession.builder.
// If you have already set the DATABRICKS_CLUSTER_ID environment variable with the
// cluster's ID, you do not also need to set the clusterId field here.
import com.databricks.connect.DatabricksSession

val spark = DatabricksSession.builder()
    .host(retrieveWorkspaceInstanceName())
    .token(retrieveToken())
    .clusterId(retrieveClusterId())
    .getOrCreate()

Python

# Use the Databricks SDK's Config class.
# If you have already set the DATABRICKS_CLUSTER_ID environment variable with the
# cluster's ID, you do not also need to set the cluster_id field here.
from databricks.connect import DatabricksSession
from databricks.sdk.core import Config

config = Config(
host       = f"https://{retrieve_workspace_instance_name()}",
token      = retrieve_token(),
cluster_id = retrieve_cluster_id()
)

spark = DatabricksSession.builder.sdkConfig(config).getOrCreate()

程式語言 Scala

// Use the Databricks SDK's Config class.
// If you have already set the DATABRICKS_CLUSTER_ID environment variable with the
// cluster's ID, you do not also need to set the clusterId field here.
import com.databricks.connect.DatabricksSession
import com.databricks.sdk.core.DatabricksConfig

val config = new DatabricksConfig()
    .setHost(retrieveWorkspaceInstanceName())
    .setToken(retrieveToken())
val spark = DatabricksSession.builder()
    .sdkConfig(config)
    .clusterId(retrieveClusterId())
    .getOrCreate()

Python

# Specify a Databricks configuration profile along with the `cluster_id` field.
# If you have already set the DATABRICKS_CLUSTER_ID environment variable with the
# cluster's ID, you do not also need to set the cluster_id field here.
from databricks.connect import DatabricksSession
from databricks.sdk.core import Config

config = Config(
profile    = "<profile-name>",
cluster_id = retrieve_cluster_id()
)

spark = DatabricksSession.builder.sdkConfig(config).getOrCreate()

程式語言 Scala

// Specify a Databricks configuration profile along with the clusterId field.
// If you have already set the DATABRICKS_CLUSTER_ID environment variable with the
// cluster's ID, you do not also need to set the clusterId field here.
import com.databricks.connect.DatabricksSession
import com.databricks.sdk.core.DatabricksConfig

val config = new DatabricksConfig()
    .setProfile("<profile-name>")
val spark = DatabricksSession.builder()
    .sdkConfig(config)
    .clusterId(retrieveClusterId())
    .getOrCreate()

Databricks 組態配置檔

在此選項中,請建立或識別一個 Azure Databricks configuration profile,包含欄位 cluster_id 以及你想使用的 Databricks 認證類型所需的其他欄位。

每個驗證類型的必要組態設定檔欄位如下所示:

然後透過組態類別設定此組態配置檔的名稱。

您可以透過幾種方式指定 cluster_id

  • cluster_id 字段加入您的組態配置檔中,然後僅指定組態配置檔的名稱。
  • 指定組態配置檔名稱以及 cluster_id 欄位。

如果您已經使用叢集識別元來設定 DATABRICKS_CLUSTER_ID 環境變數,則不需要指定 cluster_id

下列每個方法的程式代碼如下:

Python

# Include the cluster_id field in your configuration profile, and then
# just specify the configuration profile's name:
from databricks.connect import DatabricksSession

spark = DatabricksSession.builder.profile("<profile-name>").getOrCreate()

程式語言 Scala

// Include the cluster_id field in your configuration profile, and then
// just specify the configuration profile's name:
import com.databricks.connect.DatabricksSession
import com.databricks.sdk.core.DatabricksConfig

val config = new DatabricksConfig()
    .setProfile("<profile-name>")
    val spark = DatabricksSession.builder()
    .sdkConfig(config)
    .getOrCreate()

Python

# Specify the configuration profile name along with the cluster_id field.
# In this example, retrieve_cluster_id() assumes some custom implementation that
# you provide to get the cluster ID from the user or from some other
# configuration store:
from databricks.connect import DatabricksSession
from databricks.sdk.core import Config

config = Config(
profile    = "<profile-name>",
cluster_id = retrieve_cluster_id()
)

spark = DatabricksSession.builder.sdkConfig(config).getOrCreate()

程式語言 Scala

// Specify a Databricks configuration profile along with the clusterId field.
// If you have already set the DATABRICKS_CLUSTER_ID environment variable with the
// cluster's ID, you do not also need to set the clusterId field here.
import com.databricks.connect.DatabricksSession
import com.databricks.sdk.core.DatabricksConfig

val config = new DatabricksConfig()
    .setProfile("<profile-name>")
val spark = DatabricksSession.builder()
    .sdkConfig(config)
    .clusterId(retrieveClusterId())
    .getOrCreate()

DATABRICKS_CONFIG_PROFILE環境變數

在此選項中,請建立或識別一個 Azure Databricks configuration profile,包含欄位 cluster_id 以及你想使用的 Databricks 認證類型所需的其他欄位。

如果您已經使用叢集識別元來設定 DATABRICKS_CLUSTER_ID 環境變數,則不需要指定 cluster_id

每個驗證類型的必要組態設定檔欄位如下所示:

DATABRICKS_CONFIG_PROFILE 環境變數設定為此組態配置檔的名稱。 然後初始化 DatabricksSession 類別:

Python

from databricks.connect import DatabricksSession

spark = DatabricksSession.builder.getOrCreate()

程式語言 Scala

import com.databricks.connect.DatabricksSession

val spark = DatabricksSession.builder().getOrCreate()

每個組態屬性的環境變數

針對此選項,請設定 DATABRICKS_CLUSTER_ID 環境變數,以及您想要使用的 Databricks 驗證 類型所需的任何其他環境變數。

每個驗證類型的必要環境變數如下:

然後初始化 DatabricksSession 類別:

Python

from databricks.connect import DatabricksSession

spark = DatabricksSession.builder.getOrCreate()

程式語言 Scala

import com.databricks.connect.DatabricksSession

val spark = DatabricksSession.builder().getOrCreate()

名為 DEFAULT 的 Databricks 的組態配置檔

在此選項中,請建立或識別一個 Azure Databricks configuration profile,包含欄位 cluster_id 以及你想使用的 Databricks 認證類型所需的其他欄位。

如果您已經使用叢集識別元來設定 DATABRICKS_CLUSTER_ID 環境變數,則不需要指定 cluster_id

每個驗證類型的必要組態設定檔欄位如下所示:

將此組態設定檔 DEFAULT命名為 。

然後初始化 DatabricksSession 類別:

Python

from databricks.connect import DatabricksSession

spark = DatabricksSession.builder.getOrCreate()

程式語言 Scala

import com.databricks.connect.DatabricksSession

val spark = DatabricksSession.builder().getOrCreate()

設定與無伺服器計算的連線

Databricks Connect for Python 和 Scala 支援連接無伺服器運算。 若要使用這項功能,必須符合連線到無伺服器的版本需求。 請參閱 Databricks Connect 使用需求

重要

這項功能有下列限制:

對於 Python,你可以在本地環境中設定連接到無伺服器運算:

  • 將本機環境變數 DATABRICKS_SERVERLESS_COMPUTE_ID 設定為 auto。 如果設定此環境變數,Databricks Connect 會忽略 cluster_id

  • 在本機 Databricks 組態設定檔中,設定 serverless_compute_id = auto,然後從您的程式代碼參考該配置檔。

    [DEFAULT]
    host = https://my-workspace.cloud.databricks.com/
    serverless_compute_id = auto
    token = dapi123...
    

或者使用 Python 或 Scala:

Python

from databricks.connect import DatabricksSession

spark = DatabricksSession.builder.serverless().getOrCreate()
from databricks.connect import DatabricksSession

spark = DatabricksSession.builder.remote(serverless=True).getOrCreate()

程式語言 Scala

import com.databricks.connect.DatabricksSession

val spark = DatabricksSession.builder.serverless().getOrCreate()

驗證 Databricks 的連線

若要驗證您的環境、預設認證和計算連線是否已正確設定 Databricks Connect,請執行命令 databricks-connect test

databricks-connect test

當此命令偵測到安裝程式中有任何不相容時,例如當 Databricks Connect 版本與 Databricks 無伺服器計算版本不相容時,此命令會失敗,並顯示非零結束碼和對應的錯誤訊息。 如需 Databricks Connect 版本支援資訊,請參閱 Databricks Connect 版本

在 Databricks Connect 14.3 和更新版本中,您也可以使用 validateSession()來驗證您的環境:

DatabricksSession.builder.validateSession(True).getOrCreate()

停用 Databricks Connect

Databricks Connect 和基礎的 Spark Connect 服務可以在任何給定的叢集上停用。

若要停用 Databricks Connect 服務,請在叢集上設定下列 Spark 組態

spark.databricks.service.server.enabled false