从 Databricks 数据湖中的文件夹中读取最新文件

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

我有以下代码:

directory_path = "dbfs:/mnt/x_file_directory"
files = dbutils.fs.ls(directory_path)
latest_file = max(files, key=lambda f:f.modificationTime)
latest_file_path = latest_file.path
df = spark.read.option("header", "true").option("inferSchema", "true") \
  .csv(latest_file_path).toPandas()

我收到错误消息:

AnalysisException: [PATH_NOT_FOUND] Path does not exist: dbfs:/mnt/x_file_directory/file_name

文件路径看起来正确,那么我哪里出错了?预先感谢!

python pyspark databricks azure-data-lake
1个回答
0
投票

我已尝试以下方法从 ADLS 文件夹中读取最新文件:

我使用以下代码安装了我的 ADLS 容器:

dbutils.fs.mount(
    source="wasbs://<containerName>@<storageaccountName>.blob.core.windows.net/",
    mount_point="/mnt/<mountName>",
    extra_configs={
f"fs.azure.account.key.<storageaccountName>.blob.core.windows.net":"<Access-Key>"  
  }
)

enter image description here

我使用以下代码从数据湖文件夹中读取最新文件:

directory_path = "dbfs:/mnt/files/input"
files = dbutils.fs.ls(directory_path)
latest_file = max(files, key=lambda f:f.modificationTime)
latest_file_path = latest_file.path
df = spark.read.option("header", "true").option("inferSchema", "true") \
      .csv(latest_file_path).toPandas()
print(df)

enter image description here

根据this

dbfs:/mnt/
中的文件夹实际上并不是挂载的卷,而只是简单的文件夹。这就是为什么我使用
display(dbutils.fs.mounts())
检查挂载点的位置,并发现它位于存储帐户,如下所述:

enter image description here

请检查您的挂载点是否位于存储帐户中。

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