重要
装载是旧的访问模式。 Databricks 建议使用 Unity Catalog 来管理所有数据访问权限。 请参阅使用 Unity Catalog 连接到云对象存储和服务。
Azure Databricks 使用户能够将云对象存储装载到 Databricks 文件系统 (DBFS),以简化不熟悉云概念的用户的数据访问模式。 装载的数据不适用于 Unity Catalog,Databricks 建议不要使用挂载迁移,而是使用 Unity Catalog 管理数据治理。
Azure Databricks 装载在工作区和云对象存储之间创建一个链接,这使你能够使用与 Databricks 文件系统相关的熟悉文件路径与云对象存储进行交互。 装载的工作原理是,在存储以下信息的 /mnt
目录中创建本地别名:
- 云对象存储的位置。
- 用于连接到存储帐户或容器的驱动程序规范。
- 访问数据所需的安全凭据。
source
指定对象存储的 URI(并且可以选择对安全凭据进行编码)。 mount_point
指定 /mnt
目录中的本地路径。 某些对象存储源支持可选的 encryption_type
参数。 对于某些访问模式,你可以将其他配置规范作为字典传递给 extra_configs
。
注意
Databricks 建议使用 extra_configs
将特定于装载的 Spark 和 Hadoop 配置设置为选项。 这样可以确保配置绑定到装载而不是群集或会话。
dbutils.fs.mount(
source: str,
mount_point: str,
encryption_type: Optional[str] = "",
extra_configs: Optional[dict[str:str]] = None
)
在配置或更改数据装载之前,请咨询工作区和云管理员,因为配置不当可能会为工作区中的所有用户提供不安全的访问。
注意
除本文中所述的方法外,还可使用 Databricks Terraform 提供程序和 databricks_mount 自动装载存储桶。
若要卸载装入点,请使用以下命令:
dbutils.fs.unmount("/mnt/<mount-name>")
警告
为了避免错误,切勿在其他作业正在读取或写入装入点时修改装入点。 修改装载后,请始终在所有其他正在运行的群集上运行 dbutils.fs.refreshMounts()
,以传播任何装载更新。 请参阅 refreshMounts 命令 (dbutils.fs.refreshMounts)。
你可以使用 Microsoft Entra ID 应用程序服务主体进行身份验证,在 Azure 存储帐户中装载数据。 有关详细信息,请参阅使用服务主体和 Microsoft Entra ID (Azure Active Directory) 访问存储。
重要
- Azure Databricks 工作区中的所有用户都有权访问已装载的 ADLS Gen2 帐户。 应仅向用于访问 ADLS Gen2 帐户的服务主体授予对该 ADLS Gen2 帐户的访问权限,不应授予对其他 Azure 资源的访问权限。
- 通过群集创建装入点后,群集用户可立即访问装入点。 若要在另一个正在运行的群集中使用装入点,则必须在运行的群集上运行
dbutils.fs.refreshMounts()
,使新创建的装入点可供使用。 - 在作业运行时卸载装入点可能会导致错误。 确保生产作业不会在处理过程中卸载存储。
- 使用机密的装入点不会自动刷新。 如果装载的存储依赖于轮换、过期或删除的机密,则可能会发生错误,例如
401 Unauthorized
。 要解决此类错误,必须卸载并重新装载存储。 - 必须启用分层命名空间 (HNS),才能使用 ABFS 终结点成功装载 Azure Data Lake Storage Gen2 存储帐户。
在笔记本中运行以下命令,以验证并创建装入点。
configs = {"fs.azure.account.auth.type": "OAuth",
"fs.azure.account.oauth.provider.type": "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
"fs.azure.account.oauth2.client.id": "<application-id>",
"fs.azure.account.oauth2.client.secret": dbutils.secrets.get(scope="<scope-name>",key="<service-credential-key-name>"),
"fs.azure.account.oauth2.client.endpoint": "https://login.chinacloudapi.cn/<directory-id>/oauth2/token"}
# Optionally, you can add <directory-name> to the source URI of your mount point.
dbutils.fs.mount(
source = "abfss://<container-name>@<storage-account-name>.dfs.core.chinacloudapi.cn/",
mount_point = "/mnt/<mount-name>",
extra_configs = configs)
val configs = Map(
"fs.azure.account.auth.type" -> "OAuth",
"fs.azure.account.oauth.provider.type" -> "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
"fs.azure.account.oauth2.client.id" -> "<application-id>",
"fs.azure.account.oauth2.client.secret" -> dbutils.secrets.get(scope="<scope-name>",key="<service-credential-key-name>"),
"fs.azure.account.oauth2.client.endpoint" -> "https://login.chinacloudapi.cn/<directory-id>/oauth2/token")
// Optionally, you can add <directory-name> to the source URI of your mount point.
dbutils.fs.mount(
source = "abfss://<container-name>@<storage-account-name>.dfs.core.chinacloudapi.cn/",
mountPoint = "/mnt/<mount-name>",
extraConfigs = configs)
Replace
- 将
<application-id>
替换为 Azure Active Directory 应用程序的“应用程序(客户端) ID”。 - 将
<scope-name>
替换为 Databricks 机密范围名称。 - 将
<service-credential-key-name>
替换为包含客户端密码的密钥的名称。 - 将
<directory-id>
替换为 Azure Active Directory 应用程序的“目录(租户) ID”。 - 将
<container-name>
替换为 ADLS Gen2 存储帐户中的容器的名称。 - 将
<storage-account-name>
替换为 ADLS Gen2 存储帐户名称。 - 将
<mount-name>
替换为 DBFS 中的预期装入点的名称。