簡體   English   中英

訪問來自 Azure Data Lake 敏感存儲的數據塊文件

[英]Acces file from Azure Data Lake sensitive storage by databricks

我通過以下方法訪問普通存儲中的文件:

input_path = "my_path"
file= "file.mp3"
path = os.path.join(path_data, file)
full_path = '/dbfs/' + path

with open(full_path, mode='rb') as file: # b is important -> binary
    fileContent = file.read()

我無法在敏感存儲中使用相同的方法

我知道敏感存儲有另一種訪問數據的方式

path_sensitive_storage = 'mypath_sensitive'

如果我使用火花它可以完美地工作,但我有興趣不使用火花讀取而是打開文件

input_df = (spark.read
            .format("binaryFile")
            .option("header", "true")
            .option("encoding", "UTF-8")
            .csv(full_path)
            )

有辦法做到這一點嗎?

由於您使用 Azure Data Lake 作為源,您需要使用 OAuth 方法將容器掛載到 Databricks DBFS 中。 安裝容器后,您可以使用它。

使用下面的代碼安裝容器。

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": "ba219eb4-0250-4780-8bd3-d7f3420dab6d",
       "fs.azure.account.oauth2.client.secret": "0wP8Q~qWUwGSFrjyByvwK-.HjrHx2EEvG06X9cmy",
       "fs.azure.account.oauth2.client.endpoint": "https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/oauth2/token",
       "fs.azure.createRemoteFileSystemDuringInitialization": "true"}

dbutils.fs.mount(
source = "abfss://sample11@utrolicstorage11.dfs.core.windows.net/",
mount_point = "/mnt/sampledata11",
extra_configs = configs)

掛載后,您可以使用以下代碼列出掛載位置的文件。

dbutils.fs.ls("/mnt/sampledata11/")

最后使用with open語句讀取文件

with open("/dbfs/mnt/sampledata11/movies.csv", mode='rb') as file: # b is important -> binary
    fileContent = file.read()
    print(fileContent)

檢查下面的圖像以獲取完整的實現和下面的輸出。

在此處輸入圖像描述

暫無
暫無

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

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