簡體   English   中英

如何將 Azure Data Lake Store gen 2 文件共享與 Azure Databricks 連接?

[英]How to connect Azure Data Lake Store gen 2 File Share with Azure Databricks?

我有一個 Azure 數據湖存儲第 2 代帳戶,啟用了分層命名空間。 我為該帳戶生成了一個 SAS 令牌,並將數據接收到文件共享(文件服務)中的一個文件夾中。 現在我想通過 Azure Databricks 和 python 訪問這些文件。 但是,似乎 Azure Databricks 只能訪問文件系統(在 gen1 中稱為 Blob 容器),而不能訪問文件共享。 我也未能為文件系統生成 SAS 令牌。

我希望有一個可以生成 SAS 令牌並提供給我的客戶的存儲實例,並使用 python 從 azure databricks 訪問它。 只要它以某種方式工作,它是文件系統、文件共享、ADLS gen2 還是 gen1 並不重要。

我使用以下內容從數據塊訪問文件系統:

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 令牌,如下所示:

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 令牌和 azure 數據塊訪問?

從數據塊連接到 azure 文件共享的步驟

首先使用 Databricks 中的 pip install 為 Python 安裝 Microsoft Azure 存儲文件共享客戶端庫。 https://pypi.org/project/azure-storage-file-share/

安裝后,創建一個存儲帳戶。 然后您可以從數據塊創建文件共享

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://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string

通過數據塊將文件上傳到文件共享的代碼

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM