Azure データレイク ストレージと Blob Storage に接続する
この記事では、Azure Databricksから データレイクStorageおよびBlob Storageに接続する方法について説明します。
- 従来の Windows Azure ストレージ BLOB ドライバー (WASB) は非推奨になりました。 ABFSには、WASBに比べて多くの利点があります。 ABFS に関する Azure のドキュメントを参照してください。レガシ WASB ドライバーの操作に関するドキュメントについては、「 WASB を使用して Azure Blob Storage に接続する (レガシ)」を参照してください。
Azure資格情報を使用してAzureデータレイク Storage または Blob Storage に接続する
次の資格情報を使用して、 Azure データレイク ストレージまたは Blob Storage にアクセスできます。
-
OAuth 2.0 with a Microsoft Entra ID サービスプリンシパル : Databricks Microsoft Entra ID サービスプリンシパルを使用して Azure データレイク Storage に接続することをお勧めします。Microsoft Entra ID サービスプリンシパルを作成し、Azure ストレージ アカウントへのアクセスを提供するには、「サービスプリンシパル & Microsoft Entra ID(Azure Active Directory) を使用したストレージへのアクセス」を参照してください。
Microsoft Entra ID サービスプリンシパルを作成するには、
Application Administrator
ロールまたは Microsoft Entra ID のApplication.ReadWrite.All
権限が必要です。ストレージ アカウントにロールを割り当てるには、所有者であるか、ストレージ アカウントのユーザー アクセス管理者 Azure RBAC ロールを持つユーザーである必要があります。
Blob Storage は、 Microsoft Entra ID サービスプリンシパルをサポートしていません。
-
共有アクセス署名 (SAS): ストレージSAS トークンを使用してAzureストレージにアクセスできます。SASでは、きめ細かなアクセス制御を備えた一時トークンを使用して、ストレージアカウントへのアクセスを制限できます。
SASトークンに付与できるのは、ストレージアカウント、コンテナ、またはファイルに対して自分自身が持っている権限のみです。
-
アカウント keys : ストレージ アカウント アクセス キー を使用して、 Azure Storage へのアクセスを管理できます。 ストレージ アカウント アクセス キーを使用すると、ストレージ アカウントの構成とデータへのフル アクセスが提供されます。 Databricks では、アカウント キーの代わりに Microsoft Entra ID サービスプリンシパルまたは SAS トークンを使用して Azure ストレージに接続することをお勧めします。
アカウントのアクセスキーを表示するには、ストレージアカウントの所有者、コントリビュータ、またはストレージアカウントキーオペレーターサービスのロールを持っている必要があります。
Databricks では、すべての資格情報の保存にシークレットスコープを使用することをお勧めします。 ワークスペース内のユーザー、サービスプリンシパル、およびグループに、シークレットスコープを読み取るためのアクセス権を付与できます。 これにより、Azure の資格情報が保護され、ユーザーは Azure ストレージにアクセスできるようになります。 シークレットスコープを作成するには、「 シークレットスコープの管理」を参照してください。
Spark のプロパティを設定して、Azure ストレージにアクセスするための Azure 資格情報を構成します
Spark プロパティを設定して、Azure ストレージにアクセスするための Azure 資格情報を構成できます。 資格情報のスコープは、クラスターまたはノートブックのいずれかに設定できます。 クラスターアクセス制御とノートブックアクセス制御の両方を一緒に使用して、 Azure ストレージへのアクセスを保護します。 コンピュートの権限とDatabricksノートブックを使用した共同作業を参照してください。
Sparkプロパティを設定するには、クラスターのSpark構成またはノートブックで次のスニペットを使用します。
- Azure service principal
- SAS tokens
- Account key
Use the following format to set the cluster Spark configuration:
spark.hadoop.fs.azure.account.auth.type.<storage-account>.dfs.core.windows.net OAuth
spark.hadoop.fs.azure.account.oauth.provider.type.<storage-account>.dfs.core.windows.net org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider
spark.hadoop.fs.azure.account.oauth2.client.id.<storage-account>.dfs.core.windows.net <application-id>
spark.hadoop.fs.azure.account.oauth2.client.secret.<storage-account>.dfs.core.windows.net {{secrets/<secret-scope>/<service-credential-key>}}
spark.hadoop.fs.azure.account.oauth2.client.endpoint.<storage-account>.dfs.core.windows.net https://login.microsoftonline.com/<directory-id>/oauth2/token
You can use spark.conf.set
in notebooks, as shown in the following example:
service_credential = dbutils.secrets.get(scope="<secret-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")
Replace
<secret-scope>
with the Databricks secret scope name.<service-credential-key>
with the name of the key containing the client secret.<storage-account>
with the name of the Azure storage account.<application-id>
with the Application (client) ID for the Microsoft Entra ID application.<directory-id>
with the Directory (tenant) ID for the Microsoft Entra ID application.
You can configure SAS tokens for multiple storage accounts in the same Spark session.
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", dbutils.secrets.get(scope="<scope>", key="<sas-token-key>"))
Replace
<storage-account>
with the Azure Storage account name.<scope>
with the Databricks secret scope name.<sas-token-key>
with the name of the key containing the Azure storage SAS token.
spark.conf.set(
"fs.azure.account.key.<storage-account>.dfs.core.windows.net",
dbutils.secrets.get(scope="<scope>", key="<storage-account-access-key>"))
Replace
<storage-account>
with the Azure Storage account name.<scope>
with the Databricks secret scope name.<storage-account-access-key>
with the name of the key containing the Azure storage account access key.
Azure ストレージへのアクセス
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');
ノートブックの例
ADLS OAuth 2.0 with Microsoft Entra ID (formerly Azure Active Directory) サービスプリンシパル ノートブック
Azure データレイク Storage の既知の問題
Azureポータルを使用して作成されたストレージコンテナにアクセスしようとすると、次のエラーが表示されることがあります。
StatusCode=404
StatusDescription=The specified filesystem does not exist.
ErrorCode=FilesystemNotFound
ErrorMessage=The specified filesystem does not exist.
階層型名前空間が有効になっている場合は、Azureポータルを使用してコンテナを作成する必要はありません。この問題が発生した場合は、AzureポータルからBLOBコンテナを削除してください。数分後にコンテナにアクセスできるようになります。あるいは、このコンテナがAzureポータルを通じて作成されていない限り、 abfss
URIを変更して別のコンテナを使用することもできます。
Azureドキュメントの「 データレイク ストレージの既知の問題 Microsoft」を参照してください。
Databricks からのデータの保存とアクセスに関する非推奨のパターン
非推奨のストレージパターンを次に示します。
- Databricks では、外部データの場所を Databricks Filesystem にマウントすることは推奨されなくなりました。 「Databricks でのクラウド オブジェクト ストレージのマウント」を参照してください。