PathNotFound 错误消息:openFileForRead 必须与文件而不是目录一起使用
PathNotFound error message: openFileForRead must be used with files and not directories
我正在使用以下代码使用 Azure databricks 读取 excel 文件:
dfSource = spark \
.read \
.format("com.crealytics.spark.excel") \
.option("Header", "true") \
.option("inferSchema", "true") \
.load(sSourcePath)#.withColumn("SourceFile",F.input_file_name())
但是我得到以下错误:
PathNotFound error message: openFileForRead must be used with files and not directories
当我给出完整的文件名时,Spark 正确地读取了文件。
注意:我想读取文件夹中的所有文件。
如果您需要从一个目录中读取多个 Excel 文件,那么您只需要遍历这些文件,将每个文件读入一个数据帧,然后合并所有这些数据帧。在 Databricks 上,您可以使用 dbutils.fs.ls 函数列出给定目录中的文件,如下所示:
all_data = None
sSourcePath = "path_to_directory"
for f in dbutils.fs.ls(sSourcePath):
if not f.isFile or not f.name.endswith(".xlsx"):
continue
df = spark \
.read \
.format("com.crealytics.spark.excel") \
.option("Header", "true") \
.option("inferSchema", "true") \
.load(f.path)
if all_data:
all_data = all_data.union(df)
else:
all_data = df
数据帧有可能在结构上不兼容,在这种情况下,您可能需要进行一些显式转换等。
我正在使用以下代码使用 Azure databricks 读取 excel 文件:
dfSource = spark \
.read \
.format("com.crealytics.spark.excel") \
.option("Header", "true") \
.option("inferSchema", "true") \
.load(sSourcePath)#.withColumn("SourceFile",F.input_file_name())
但是我得到以下错误:
PathNotFound error message: openFileForRead must be used with files and not directories
当我给出完整的文件名时,Spark 正确地读取了文件。
注意:我想读取文件夹中的所有文件。
如果您需要从一个目录中读取多个 Excel 文件,那么您只需要遍历这些文件,将每个文件读入一个数据帧,然后合并所有这些数据帧。在 Databricks 上,您可以使用 dbutils.fs.ls 函数列出给定目录中的文件,如下所示:
all_data = None
sSourcePath = "path_to_directory"
for f in dbutils.fs.ls(sSourcePath):
if not f.isFile or not f.name.endswith(".xlsx"):
continue
df = spark \
.read \
.format("com.crealytics.spark.excel") \
.option("Header", "true") \
.option("inferSchema", "true") \
.load(f.path)
if all_data:
all_data = all_data.union(df)
else:
all_data = df
数据帧有可能在结构上不兼容,在这种情况下,您可能需要进行一些显式转换等。