How to export a table dataframe in PySpark to csv?

后端 未结 5 847
半阙折子戏
半阙折子戏 2020-11-27 02:33

I am using Spark 1.3.1 (PySpark) and I have generated a table using a SQL query. I now have an object that is a DataFrame. I want to export this DataFrame

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 03:15

    How about this (in you don't want an one liner) ?

    for row in df.collect():
        d = row.asDict()
        s = "%d\t%s\t%s\n" % (d["int_column"], d["string_column"], d["string_column"])
        f.write(s)
    

    f is a opened file descriptor. Also the separator is a TAB char, but it's easy to change to whatever you want.

提交回复
热议问题