gpt4 book ai didi

azure - 如何将 Azure Data Lake Storage gen2 文件共享与 Azure Databricks 连接?

转载 作者:行者123 更新时间:2023-12-02 08:00:17 25 4
gpt4 key购买 nike

我有一个 Azure 数据湖存储第 2 代帐户,并启用了分层命名空间。我为该帐户生成了 SAS token ,并将数据接收到文件共享(文件服务)中的文件夹中。现在我想通过 Azure Databricks 和 python 访问这些文件。但是,Azure Databricks 似乎只能访问文件系统(在第 1 代中称为 Blob 容器),而不能访问文件共享。我也未能生成文件系统的 SAS token 。

我希望有一个存储实例,可以生成 SAS token 并提供给我的客户端,并使用 python 从 azure databricks 访问相同的 token 。是文件系统、文件共享、ADLS gen2 还是 gen1 并不重要,只要它能正常工作即可。

我使用以下命令从 databricks 访问文件系统:

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": "my_client_id",
"fs.azure.account.oauth2.client.secret": "my_client_secret",
"fs.azure.account.oauth2.client.endpoint": "https://login.microsoftonline.com/"+"My_tenant_id" +"/oauth2/token",
"fs.azure.createRemoteFileSystemDuringInitialization": "true"}

dbutils.fs.mount(source = "abfss://"+"my_file_system"+"@"+"my_storage_account"+".dfs.core.windows.net/MyFolder",
mount_point = "/mnt/my_mount",
extra_configs = configs)

工作正常,但我无法让它访问文件共享。我有一个带有连接字符串的 SAS token ,如下所示:

connection_string = (
'BlobEndpoint=https://<my_storage>.blob.core.windows.net/;'+
'QueueEndpoint=https://<my_storage>.queue.core.windows.net/;'+
'FileEndpoint=https://<my_storage>.file.core.windows.net/;'+
'TableEndpoint=https://<my_storage>.table.core.windows.net/;'+
'SharedAccessSignature=sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-09-26T17:12:38Z&st=2019-08-26T09:12:38Z&spr=https&sig=<my_sig>'
)

我设法使用它来将内容上传到文件共享,但不上传到文件系统。是否有任何类型的 Azure 存储可以通过 SAS token 和 azure databricks 访问?

最佳答案

从 databricks 连接到 azure 文件共享的步骤

首先使用 Databricks 中的 pip install 安装适用于 Python 的 Microsoft Azure 存储文件共享客户端库。 https://pypi.org/project/azure-storage-file-share/

安装后,创建一个存储帐户。然后您可以从 databricks 创建文件共享

from azure.storage.fileshare import ShareClient

share = ShareClient.from_connection_string(conn_str="<connection_string consists of FileEndpoint=myFileEndpoint(https://storageaccountname.file.core.windows.net/);SharedAccessSignature=sasToken>", share_name="<file share name that you want to create>")

share.create_share()

将此用于进一步引用 https://learn.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string

通过 databricks 将文件上传到文件共享的代码

from azure.storage.fileshare import ShareFileClient

file_client = ShareFileClient.from_connection_string(conn_str="<connection_string consists of FileEndpoint=myFileEndpoint(https://storageaccountname.file.core.windows.net/);SharedAccessSignature=sasToken>", share_name="<your_fileshare_name>", file_path="my_file")

with open("./SampleSource.txt", "rb") as source_file:
file_client.upload_file(source_file)

请参阅此链接了解更多信息 https://pypi.org/project/azure-storage-file-share/

关于azure - 如何将 Azure Data Lake Storage gen2 文件共享与 Azure Databricks 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57659774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com