注意
本文涵蓋 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 使用需求。
- Azure Databricks workspace 實例名稱。 這是您的計算環境中的伺服器 主機名 值。 參見 取得Azure Databricks計算資源 的連線細節。
- 如果您要連線到傳統運算,則需要您叢集的一個識別碼。 您可以從 URL 擷取叢集標識碼。 請參閱 運算資源 URL 和 ID。
設定叢集的連線
有多種方式可設定叢集的連線。 Databricks Connect 會依下列順序搜尋組態屬性,並使用它找到的第一個組態。 如需進階設定資訊,請參閱 Databricks Connect 的進階使用方式。
- DatabricksSession 類別的 remote() 方法。
- Databricks 組態配置檔
- DATABRICKS_CONFIG_PROFILE環境變數
- 每個組態屬性的環境變數
- 名為 DEFAULT 的 Databricks 組態配置檔
類別 DatabricksSession 的 remote() 方法
此選項僅適用於 Authenticate with Azure Databricks Personal Access Tokens(舊版),請指定工作區實例名稱、Azure Databricks個人存取權杖及叢集 ID。
您可以透過數種方式初始化 類別 DatabricksSession :
- 在
host中設定token、cluster_id和DatabricksSession.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 認證類型所需的其他欄位。
每個驗證類型的必要組態設定檔欄位如下所示:
- 對於 Azure Databricks 個人存取憑證認證:
host和token。 - 針對 OAuth 機器對機器(M2M)驗證(支援的位置):
host、client_id和client_secret。 - 針對 OAuth 使用者對機器 (U2M) 驗證(如果支援):
host。 - 對於Microsoft Entra ID(前稱Azure Active Directory)服務主體認證:
host、azure_tenant_id、azure_client_id、azure_client_secret,以及可能的azure_workspace_resource_id。 - 對於 Azure CLI 認證:
host。 - 對於Azure管理身份驗證(支援時):
host、azure_use_msi、azure_client_id,以及可能的azure_workspace_resource_id。
然後透過組態類別設定此組態配置檔的名稱。
您可以透過幾種方式指定 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。
每個驗證類型的必要組態設定檔欄位如下所示:
- 對於 Azure Databricks 個人存取憑證認證:
host和token。 - 針對 OAuth 機器對機器(M2M)驗證(支援的位置):
host、client_id和client_secret。 - 針對 OAuth 使用者對機器 (U2M) 驗證(如果支援):
host。 - 對於Microsoft Entra ID(前稱Azure Active Directory)服務主體認證:
host、azure_tenant_id、azure_client_id、azure_client_secret,以及可能的azure_workspace_resource_id。 - 對於 Azure CLI 認證:
host。 - 對於Azure管理身份驗證(支援時):
host、azure_use_msi、azure_client_id,以及可能的azure_workspace_resource_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 驗證 類型所需的任何其他環境變數。
每個驗證類型的必要環境變數如下:
- 對於 Azure Databricks 個人存取憑證認證:
DATABRICKS_HOST和DATABRICKS_TOKEN。 - 針對 OAuth 機器對機器(M2M)驗證(支援的位置):
DATABRICKS_HOST、DATABRICKS_CLIENT_ID和DATABRICKS_CLIENT_SECRET。 - 針對 OAuth 使用者對機器 (U2M) 驗證(如果支援):
DATABRICKS_HOST。 - 對於Microsoft Entra ID(前稱Azure Active Directory)服務主體認證:
DATABRICKS_HOST、ARM_TENANT_ID、ARM_CLIENT_ID、ARM_CLIENT_SECRET,以及可能的DATABRICKS_AZURE_RESOURCE_ID。 - 對於 Azure CLI 認證:
DATABRICKS_HOST。 - 對於Azure管理身份驗證(支援時):
DATABRICKS_HOST、ARM_USE_MSI、ARM_CLIENT_ID,以及可能的DATABRICKS_AZURE_RESOURCE_ID。
然後初始化 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。
每個驗證類型的必要組態設定檔欄位如下所示:
- 對於 Azure Databricks 個人存取憑證認證:
host和token。 - 針對 OAuth 機器對機器(M2M)驗證(支援的位置):
host、client_id和client_secret。 - 針對 OAuth 使用者對機器 (U2M) 驗證(如果支援):
host。 - 對於Microsoft Entra ID(前稱Azure Active Directory)服務主體認證:
host、azure_tenant_id、azure_client_id、azure_client_secret,以及可能的azure_workspace_resource_id。 - 對於 Azure CLI 認證:
host。 - 對於Azure管理身份驗證(支援時):
host、azure_use_msi、azure_client_id,以及可能的azure_workspace_resource_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 使用需求。
重要
這項功能有下列限制:
- Databricks Connect for Scala 支援無伺服器運算目前仍處於 測試階段。
- Databricks Connect 版本與 Python 或 Scala 版本必須相容。 請參閱 Databricks Connect 版本。
- Databricks Connect for Python 或 Databricks Connect for Scala 的所有限制。
- 所有無伺服器計算限制
對於 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