Spark - How to write a single csv file WITHOUT folder?

前端 未结 9 1224
北恋
北恋 2020-12-28 13:44

Suppose that df is a dataframe in Spark. The way to write df into a single CSV file is

df.coalesce(1).write.option(\"header\", \"tru

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-28 14:27

    If you want to use only the python standard library this is an easy function that will write to a single file. You don't have to mess with tempfiles or going through another dir.

    import csv
    
    def spark_to_csv(df, file_path):
        """ Converts spark dataframe to CSV file """
        with open(file_path, "w") as f:
            writer = csv.DictWriter(f, fieldnames=df.columns)
            writer.writerow(dict(zip(fieldnames, fieldnames)))
            for row in df.toLocalIterator():
                writer.writerow(row.asDict())
    

提交回复
热议问题