通过开放方法(abfss)访问数据湖中的文件

问题描述 投票:0回答:1

过去使用挂载点使用

open
从数据湖读取文件。现在我们不想再这样做了,而是使用外部位置路径
abfss

下面的代码不起作用。没有这样的文件或目录

with open('abfss://urlofcloudstorage/container/file.txt') as f:
data = f.read()

我刚刚意识到

open
方法仅适用于本地文件,但它无法从
abfss

读取任何内容

从数据湖读取文件的解决方案是什么。我看到了一个选项

dbutils.fs.cp
但我真的不想在本地复制文件。有什么建议吗?

更新:我也尝试过

dbutils.fs.cp
,但由于我使用共享访问模式集群,因此不支持

  def decrypt_csv_file_to_pandas(self, source_path, pgp_passphrase, csv_separator):
    """
    Decrypt a csv file directly into a pandas dataframe.
    """
    with open(source_path, 'rb') as f:
      decrypted = self.gpg.decrypt_file(
        file=f,
        passphrase= pgp_passphrase
      )
      print(decrypted.status)
      df_pd = pd.read_csv(io.StringIO(str(decrypted)) , sep=csv_separator, low_memory=False, keep_default_na=False)
      return df_pd 
azure apache-spark pyspark azure-databricks azure-data-lake-gen2
1个回答
0
投票

在 Databricks 库选项卡中安装 Python 包

adlfs
或使用以下命令:

pip install adlfs

然后,使用以下代码:

from adlfs import AzureBlobFileSystem
key = "z9XY91xxxxxxxxxxxxxxxxxyyyyyyyyyyyy"
container_name = "data"
file_path = "pdf/titanic.csv"
abfs = AzureBlobFileSystem(account_name="jadls2", account_key=key)

with abfs.open(f"{container_name}/{file_path}", "r") as f:
    print(f.read())

这里,我在配置时提供了密钥,但我不建议这样做。相反,请使用

SAS
令牌或服务主体。

检查this以获取有关不同凭据参数的更多信息。

输出:

enter image description here

我只打印文件数据。在你的情况下,你应该解密它并将其读入 pandas DataFrame。

© www.soinside.com 2019 - 2024. All rights reserved.