Sparkr write DF as file csv/txt

前端 未结 1 1398
挽巷
挽巷 2021-01-07 02:52

Hi I\'m working on sparkR in yarn mode.

I need to write a sparkr df to a csv/txt file.

I saw that there is write.df but it writes parquet files.

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 03:32

    Spark 2.0+

    You can use write.text function:

    Save the content of the SparkDataFrame in a text file at the specified path. The SparkDataFrame must have only one column of string type with the name "value". Each row becomes a new line in the output file.

    write.text(df, path)
    

    or write.df with built-in SparkR csv writer:

    write.df(df, path, source="csv")
    

    Spark 1.x

    You can use spark-csv package:

    write.df(SparkRDF, "foo.csv", "com.databricks.spark.csv", ...)
    

    It can be added for example with packages argument to SparkR / spark-submit:

    sparkR --packages com.databricks:spark-csv_2.10:1.3.0 # For Scala 2.10
    sparkR --packages com.databricks:spark-csv_2.11:1.3.0 # For Scala 2.11
    

    For other options see the official documentation

    0 讨论(0)
提交回复
热议问题