首页 > 解决方案 > 如何在 DBFS 上挂载 Azure Data Lake Store

问题描述

我需要使用 Azure 服务主体客户端凭据在 Azure Databricks 文件系统上装载 Azure Data Lake Store Gen1 数据文件夹。请帮忙

标签: azuredatabricksazure-data-lakeazure-databricks

解决方案


可通过三种方式访问​​ Azure Data Lake Storage Gen1:

  1. 传递您的 Azure Active Directory 凭据,也称为凭据传递。
  2. 使用服务主体和 OAuth 2.0 将 Azure Data Lake Storage Gen1 文件系统装载到 DBFS。
  3. 直接使用服务主体。

1. 传递您的 Azure Active Directory 凭据,也称为凭据传递:

可以使用用于登录 Azure Databricks 的相同 Azure Active Directory (Azure AD) 标识从 Azure Databricks 群集自动向 Azure Data Lake Storage Gen1 进行身份验证。为 Azure AD 凭据直通启用群集时,在该群集上运行的命令将能够读取和写入 Azure Data Lake Storage Gen1 中的数据,而无需配置服务主体凭据以访问存储。

为标准群集启用 Azure Data Lake Storage 凭据直通

在此处输入图像描述

有关完整的设置和使用说明,请参阅使用 Azure Active Directory 凭据直通安全访问 Azure Data Lake Storage

2. 使用服务主体和 OAuth 2.0 将 Azure Data Lake Storage Gen1 文件系统装载到 DBFS。

步骤 1:创建并授予服务主体权限

如果您选择的访问方法需要具有足够权限的服务主体,而您没有,请执行以下步骤:

  1. 创建可以访问资源的 Azure AD 应用程序和服务主体。请注意以下属性:

    application-id:唯一标识客户端应用程序的 ID。

    directory-id:唯一标识 Azure AD 实例的 ID。

    service-credential:应用程序用来证明其身份的字符串。

  2. 注册服务主体,在 Azure Data Lake Storage Gen1 帐户上授予正确的角色分配,例如参与者。

第 2 步:使用服务主体和 OAuth 2.0 装载 Azure Data Lake Storage Gen1 资源

Python代码:

configs = {"<prefix>.oauth2.access.token.provider.type": "ClientCredential",
           "<prefix>.oauth2.client.id": "<application-id>",
           "<prefix>.oauth2.credential": dbutils.secrets.get(scope = "<scope-name>", key = "<key-name-for-service-credential>"),
           "<prefix>.oauth2.refresh.url": "https://login.microsoftonline.com/<directory-id>/oauth2/token"}

# Optionally, you can add <directory-name> to the source URI of your mount point.
dbutils.fs.mount(
  source = "adl://<storage-resource>.azuredatalakestore.net/<directory-name>",
  mount_point = "/mnt/<mount-name>",
  extra_configs = configs)

在此处输入图像描述

3. 使用服务主体和 OAuth 2.0 通过 Spark API 直接访问

可以使用服务主体通过 OAuth 2.0 直接访问 Azure Data Lake Storage Gen1 存储帐户(而不是使用 DBFS 装载)。

使用 DataFrame API 访问:

要从您的 Azure Data Lake Storage Gen1 帐户中读取数据,您可以将 Spark 配置为使用笔记本中包含以下代码段的服务凭据:

spark.conf.set("<prefix>.oauth2.access.token.provider.type", "ClientCredential")
spark.conf.set("<prefix>.oauth2.client.id", "<application-id>")
spark.conf.set("<prefix>.oauth2.credential","<key-name-for-service-credential>"))
spark.conf.set("<prefix>.oauth2.refresh.url", "https://login.microsoftonline.com/<directory-id>/oauth2/token")

在此处输入图像描述

参考: Azure Databricks - Azure Data Lake Storage Gen1


推荐阅读