DBFS使用dbutils實現存儲服務的裝載(mount、掛載),用戶可以把Azure Data Lake Storage Gen2和Azure Blob Storage 賬戶裝載到DBFS中。Mount是Data Lake Storage Gen2的指針,因此數據不會同步到本地,但是用戶可以訪問遠程文件系統中的數據。Mount操作相當於創建了一個共享文件系統。
要實現掛載,需要5個最基本條件:
- Azure Data Lake Storage Gen2 account
- Azure Application
- Azure Key Vault
- Databricks Workspace
- Secret Scope
一,創建Azure Data Lake Storage Gen2 Account
從Azure Portal中搜索Storage Accounts服務,創建Data Lake V2 Account
1,創建Data Lake V2的詳細步驟
Step1:配置Basics選項卡
配置Storage Account使用的Subscription和Resource group;
選擇Account Kind為:StorageV2(General purpose v2);
Step2:Networking 使用默認值
Step3:Data Protection 使用默認值
Step4:配置Advanced選項卡,
啟用"Hierarchical namespace",這是Data Lake Storage Gen2的獨有功能,有層次結構的文件系統
Step5:Review+Create
2,為Data Lake Storage Gen2創建文件系統
進入到Data Lake Storage Gen2的資源頁面中,從“Tools and SDKs”中選擇“Storage Explorer”,
進入到Storage Explorer中,右擊CONTAINERS,選擇“Create file system”:
文件系統其實就是一個Directory,並可以創建子目錄。
二,注冊Application
為了在ADLS Gen 2和Azure Databricks之間建立連接,需要建立應用程序連接,因此我們需要注冊一個Azure AD application,創建一個服務主體(Service Principal),用於生成application 驗證密鑰,該密鑰稱作Client Secret,存儲在Azure Key Vault實例中。
1,注冊App
從Azure Portal中搜索“Azure Active Directory”服務,從Overview界面中選擇“App registration”,注冊一個app:
在注冊的App中輸入Name:vic_test_app,點擊“Register”,開始注冊App
在完成App的注冊之后,點擊“App registrations”,從“Owned applications”中查看擁有的App,點擊剛注冊的app:vic_test_app。
從vic_test_app的overview頁面中,我們得到該app實例的兩個重要ID:
Application (client) ID:注冊的app實例的ID
Directory (tenant) ID:app實例注冊到Azure AD tenant ID就是Directory ID。
2,為該app添加app secret(驗證密鑰),以訪問該app
使用app生成驗證密鑰(authentication key、application password、client secret 或application secret),利用該密鑰來驗證app。打開“Certificates & secrets”頁面,點擊“+New client secret”來創建新的Client Secret:
輸入Description,選擇過期日期"In 1 year",點擊“Add”生成新的client secrets:
當點擊Add時,client secret(authentication key)將會出現。你只有一次機會可以復制key-value。當你離開這個頁面時,你永遠沒有機會再獲取到key-value。
注意:復制Client Secret 的Value字段,當執行其他操作之后,這個值將無法再被查看到。
3,得到的數據
三,授予Service Principal訪問Data Lake V2賬戶的權限
我們需要為Service Principal分配訪問角色(該Service Principal是在注冊App時自動創建),以訪問存儲賬戶中的數據。
跳轉到Azure Portal主頁,打開Storage Accounts中的Data Lake Storage Gen2賬戶,點擊Access Control (IAM),在Access Control (IAM)頁面中,點擊“+ Add”,選擇“Add role assignment”。打開“Add role assignment”頁面,在Role列表中選擇“Storage Blob Data Contributor”,在“Assign access to”列表中選擇“Azure AD user、group or service principal”,在“Select”列表中選擇之前注冊的app,點擊“Save”按鈕,完成權限的授予。
四,創建Key Vault
Key Vault服務用於安全地存儲key、密碼、證書等secret,因此,我們需要把從已注冊的app中獲取到的Client Secret存儲到Key Vault中。
1:創建Key Vault
在Key Vault創建完成之后,向Key Vault中添加一個Secret,
2:保存Secret
定義Secret的Name,把從已注冊的app中獲取到的Client Secret存儲到Secret的Value中。
3,從Key Vault得到的數據
從Key Vault的Settings中點擊“Properties”:
五,創建Azure Key Vault-backed的Secret Scope
Databricks 使用Secret Scope來管理Secret,Secret Scope是由Secret構成的,該Secret是由name來唯一標識的。
Step1,導航到創建Secret Scope的頁面
根據Databricks實例,導航到創建Secret Scope的頁面,注意該URI是區分大小寫的。
https://<databricks-instance>#secrets/createScope
Step2,輸入Secret Scope的屬性
ScopeName是區分大小寫的,並且DNS Name和Resource ID都必須從Key Vault中復制。
DNS Name是Key Valut 屬性中Vault URI。
六 ,掛載Data Lake Storage Gen2
通過創建 Azure Data Lake Storage Gen2的文件系統,注冊App、創建Key Vault、創建Secret Scope,我們完成了把Data Lake Gen2掛載到DBFS的所有准備工作,並獲得了以下數據:
- Client ID (a.k.a. Application ID)
- Client Secret (a.k.a. Application Secret)
- Directory ID (a.k.a Tenant ID)
- Databricks Secret Scope Name
- Key Name for Service Credentials (from Azure Key Vault, it is the secret's name)
- File System Name
- Storage Account Name
- Mount Name
Databricks提供了掛載命令:dbutils.mount(),通過該命令,我們可以把Azure Data Lake Storage Gen2掛載到DBFS中。掛載操作是一次性的操作,一旦掛載操作完成,就可以把遠程的Data Lake Gen2的file system當作本地文件來使用。
1,掛載Azure Data Lake Storage Gen2
使用服務主體(Service Principal)和OAuth 2.0進行身份驗證,把Azure Data Lake Storage Gen2帳戶裝載到DBFS,該裝載點(mount pointer)是數據湖存儲的指針,數據不需要同步到本地,但是只要遠程文件系統中的數據有更新,我們就能獲得數據的更新。
掛載Data Lake Storage Gen2文件系統,目前只支持OAuth 2.0 Credential:
###################################################################################### # Set the configurations. Here's what you need: ## 1.) Client ID (a.k.a Application ID) ## 2.) Client Secret (a.k.a. Application Secret) ## 3.) Directory ID ## 4.) File System Name ## 5.) Storage Account Name ## 6.) Mount Name ###################################################################################### 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": "<client-id>", "fs.azure.account.oauth2.client.secret": dbutils.secrets.get(scope = "<scope-name>", key = "<key-name-for-service-credential>"), "fs.azure.account.oauth2.client.endpoint": "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 = "abfss://<file-system-name>@<storage-account-name>.dfs.core.windows.net/", mount_point = "/mnt/<mount-name>", extra_configs = configs)
參數注釋:
- <Client-id>:App ID
- <scope-name>:Secret Scope的名稱
- <key-name-for-service-credential>:Azure Key Vault
- <directory-id>:tenant Id
<mount-name>
:是DBFS path,表示Data Lake Store或其中的一個Folder在DBFS中裝載的位置dbutils.secrets.get(scope="<scope-name>",key="<service-credential-key-name>")
:從Secret Scope中的Secret中獲取服務憑證- <file-system-name>:文件系統的名稱
- <storage-account-name>:存儲賬戶的名稱
2,訪問掛載點
訪問掛載點中的文件,可以通過pyspark.sql來訪問:
df = spark.read.text("/mnt/%s/...." % <mount-name>) df = spark.read.text("dbfs:/mnt/<mount-name>/....")
或者通過SQL命令來訪問:
%sql select * from csv.`/mnt/mount_datalakeg2/stword.csv`
3,刷新掛載點
dbutils.fs.refreshMounts()
4,卸載掛載點:
dbutils.fs.unmount("/mnt/<mount-name>")
5,通過編程方式獲得相應的數據進行掛載

# Python code to mount and access Azure Data Lake Storage Gen2 Account to Azure Databricks with Service Principal and OAuth # Author: Dhyanendra Singh Rathore # Define the variables used for creating connection strings adlsAccountName = "dlscsvdataproject" adlsContainerName = "csv-data-store" adlsFolderName = "covid19-data" mountPoint = "/mnt/csvFiles" # Application (Client) ID applicationId = dbutils.secrets.get(scope="CSVProjectKeyVault",key="ClientId") # Application (Client) Secret Key authenticationKey = dbutils.secrets.get(scope="CSVProjectKeyVault",key="ClientSecret") # Directory (Tenant) ID tenandId = dbutils.secrets.get(scope="CSVProjectKeyVault",key="TenantId") endpoint = "https://login.microsoftonline.com/" + tenandId + "/oauth2/token" source = "abfss://" + adlsContainerName + "@" + adlsAccountName + ".dfs.core.windows.net/" + adlsFolderName # Connecting using Service Principal secrets and OAuth 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": applicationId, "fs.azure.account.oauth2.client.secret": authenticationKey, "fs.azure.account.oauth2.client.endpoint": endpoint} # Mounting ADLS Storage to DBFS # Mount only if the directory is not already mounted if not any(mount.mountPoint == mountPoint for mount in dbutils.fs.mounts()): dbutils.fs.mount( source = source, mount_point = mountPoint, extra_configs = configs)
參考文檔:
Mounting & accessing ADLS Gen2 in Azure Databricks using Service Principal and Secret Scopes
Mount an ADLS Gen 2 to Databricks File System Using a Service Principal and OAuth 2.0 (Ep. 5)