使用 Azure Databricks 访问 Azure Data Lake Storage Gen2 和 Blob 存储

使用 Azure Blob 文件系统驱动程序 (ABFS) 从 Azure Databricks 连接到 Azure Blob 存储和 Azure Data Lake Storage Gen2。 Databricks 建议使用群集配置中设置的 Azure 服务主体来保护对 Azure 存储容器的访问。

本文详细介绍如何使用以下方法访问 Azure 存储容器:

  • Azure 服务主体
  • SAS 令牌
  • 帐户密钥

你将设置 Spark 属性以为计算环境配置以下凭据之一:

  • 范围为 Azure Databricks 群集
  • 范围为 Azure Databricks 笔记本

还可以使用 Azure 服务主体从 Databricks SQL 访问 Azure 存储;请参阅配置对云存储的访问权限

Databricks 建议使用机密范围来存储所有凭据。

用于存储和访问 Azure Databricks 中数据的弃用模式

Databricks 不再建议将外部数据位置装载到 Databricks 文件系统;请参阅在 Azure Databricks 上装载云对象存储

Databricks 不再建议使用 Azure Active Directory 凭据传递访问 Azure Data Lake Storage

旧版 Windows Azure 存储 Blob 驱动程序 (WASB) 已弃用。 与 WASB 相比,ABFS 有很多好处。请参阅有关 ABFS 的 Azure 文档。 有关使用旧版 WASB 驱动程序的文档,请参阅使用 WASB(旧版)连接到 Azure Blob 存储

Azure 已宣布 Azure Data Lake Storage Gen1 即将停用。 Azure Databricks 建议将所有 Azure Data Lake Storage Gen1 迁移到 Azure Data Lake Storage Gen2。 如果尚未迁移,请参阅从 Azure Databricks 访问 Azure Data Lake Storage Gen1

使用 Blob 存储或 Azure Data Lake Storage Gen2 的 ABFS URI 直接访问

如果已正确配置用于访问 Azure 存储容器的凭据,则可以使用 URI 与存储帐户中的资源进行交互。 Databricks 建议使用 abfss 驱动程序以获得更高的安全性。

spark.read.load("abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/<path-to-data>")

dbutils.fs.ls("abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/<path-to-data>")
CREATE TABLE <database-name>.<table-name>;

COPY INTO <database-name>.<table-name>
FROM 'abfss://container@storageAccount.dfs.core.windows.net/path/to/folder'
FILEFORMAT = CSV
COPY_OPTIONS ('mergeSchema' = 'true');

将 OAuth 2.0 与 Azure 服务主体配合使用来访问 Azure Data Lake Storage Gen2 或 Blob 存储

可将 OAuth 2.0 与 Azure Active Directory (Azure AD) 应用程序服务主体配合使用来安全地访问 Azure 存储帐户中的数据,以便进行身份验证;请参阅使用 Azure Active Directory 服务主体配置对 Azure 存储的访问权限

service_credential = dbutils.secrets.get(scope="<scope>",key="<service-credential-key>")

spark.conf.set("fs.azure.account.auth.type.<storage-account>.dfs.core.windows.net", "OAuth")
spark.conf.set("fs.azure.account.oauth.provider.type.<storage-account>.dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider")
spark.conf.set("fs.azure.account.oauth2.client.id.<storage-account>.dfs.core.windows.net", "<application-id>")
spark.conf.set("fs.azure.account.oauth2.client.secret.<storage-account>.dfs.core.windows.net", service_credential)
spark.conf.set("fs.azure.account.oauth2.client.endpoint.<storage-account>.dfs.core.windows.net", "https://login.microsoftonline.com/<directory-id>/oauth2/token")

替换

  • <scope> 替换为 Databricks 机密范围名称。
  • <service-credential-key> 替换为包含客户端密码的密钥的名称。
  • <storage-account> 替换为 Azure 存储帐户的名称。
  • <application-id> 替换为 Azure Active Directory 应用程序的“应用程序(客户端) ID”。
  • <directory-id> 替换为 Azure Active Directory 应用程序的“目录(租户) ID”。

使用 SAS 令牌访问 Azure Data Lake Storage Gen2 或 Blob 存储

你可以使用存储共享访问签名 (SAS) 直接访问 Azure Data Lake Storage Gen2 存储帐户。 使用 SAS,你可以通过具有细粒度访问控制的临时令牌来限制对存储帐户的访问。

可以在同一个 Spark 会话中为多个存储帐户配置 SAS 令牌。

注意

Databricks Runtime 7.5 及更高版本中提供了 SAS 支持。

spark.conf.set("fs.azure.account.auth.type.<storage-account>.dfs.core.windows.net", "SAS")
spark.conf.set("fs.azure.sas.token.provider.type.<storage-account>.dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.sas.FixedSASTokenProvider")
spark.conf.set("fs.azure.sas.fixed.token.<storage-account>.dfs.core.windows.net", "<token>")

使用帐户密钥访问 Azure Data Lake Storage Gen2 或 Blob 存储

可以使用存储帐户访问密钥来管理对 Azure 存储的访问权限。

spark.conf.set(
    "fs.azure.account.key.<storage-account>.dfs.core.windows.net",
    dbutils.secrets.get(scope="<scope>", key="<storage-account-access-key>"))

替换

  • <storage-account> 替换为 Azure 存储帐户名称。
  • <scope> 替换为 Azure Databricks 机密范围名称。
  • <storage-account-access-key> 替换为包含 Azure 存储帐户访问密钥的密钥的名称。

示例笔记本

此笔记本演示如何使用服务主体执行以下操作:

  1. 验证 ADLS Gen2 存储帐户。
  2. 在存储帐户中装载文件系统。
  3. 将包含物联网 (IoT) 数据的 JSON 文件写入新容器。
  4. 使用直接访问和通过装入点列出文件。
  5. 使用直接访问和通过装入点来读取和显示 IoT 文件。

具有 Azure 服务主体笔记本的 ADLS Gen2 OAuth 2.0

获取笔记本

Azure Data Lake Storage Gen2 常见问题解答和已知问题

请参阅 Azure Data Lake Storage Gen2 常见问题解答和已知问题