在spark中读取多个xml文件时获取filename



我正在尝试使用读取spark中的多个xml文件

spark.read.text(path/*.xml,wholetext=true)

之后,我将使用python中的xmlTree库对它们进行解析。不幸的是,我的一些xml文件是错误的,并且缺少一些结束标记。因此,运行此命令会出现错误。我想知道哪个文件有问题。我似乎在错误消息中找不到文件。有没有办法知道哪个文件是错的?

p.S我不能使用spark-xml开源库,因为它不能为我的用例提供正确的结果。

您将尝试input_file_namehttps://spark.apache.org/docs/latest/api/python/reference/api/pyspark.sql.functions.input_file_name.html?highlight=input_file#pyspark.sql.functions.input_file_name

(spark
.read
.csv('*.csv', header=True)
.withColumn('file', F.input_file_name())
.show(10, False)
)
# +---+---+---+------------------------------------------------+
# |a  |b  |c  |file                                            |
# +---+---+---+------------------------------------------------+
# |1  |1  |1  |file:///Volumes/A/B/C/a.csv                     |
# |2  |2  |2  |file:///Volumes/A/B/C/a1.csv                    |
# +---+---+---+------------------------------------------------+

最新更新