Azure数据块:如何读取零件文件并将其另存为一个文件到Blob?

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

我正在使用Python Spark将数据帧写入blob中的文件夹,该文件夹将另存为零件文件:

df.write.format("json").save("/mnt/path/DataModel")

文件另存为:

enter image description here

我正在使用以下代码将其合并到一个文件中:

#Read Part files
 path = glob.glob("/dbfs/mnt/path/DataModel/part-000*.json")  

#Move file to FinalData folder in blbo
    for file in path: 
          shutil.move(file,"/dbfs/mnt/path/FinalData/FinalData.json")

但是FinalData.Json仅具有最后一部分文件数据,而没有全部数据零件文件。

python azure apache-spark databricks azure-databricks
1个回答
0
投票

[我看到您只想将这些文件的内容合并到一个文件中,但是由于如下图对shutil.move函数的描述,其功能类似于Linux mv,因此最后一个文件的内容将涵盖先前文件的内容。

enter image description here

并且通过代码写入多个文件的原因是Spark在HDFS上运行,因此在HDFS上写入的128MB(HDFS部件文件大小)以上的数据将生成多个以part前缀命名的文件,请参阅[C0 ]。

一种满足您需要的解决方法是将PySpark数据帧转换为Pandas数据帧,然后使用pandas数据帧函数What is Small file problem in HDFS ?编写json文件。

这是我的示例代码。

to_json

然后检查文件是否存在。

df.toPandas().to_json('/dbfs/mnt/path/FinalData/FinalData.json')

import os
os.path.isfile('/dbfs/mnt/path/FinalData/FinalData.json')

作为参考,下图是我的结果。

dbutils.fs.ls('dbfs:/mnt/path/')

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