<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: When I save a Spark dataframe using df.write.format(&amp;quot;csv&amp;quot;), I end up with mulitple csv files. Why is this happening? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21583#M14729</link>
    <description>&lt;P&gt;You get multiple files in a folder because spark writes each shuffle partition in-place out to a "part..." file to avoid network I/O. You can use coalesce to bring all the shuffles into a single partition and write it out to a single file but be mindful of the performance implications&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df.coalesce(1)
   .write.format(csv)
   .option("header", "true")
   .save("singlefile.csv")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 23 Jun 2021 03:13:38 GMT</pubDate>
    <dc:creator>aladda</dc:creator>
    <dc:date>2021-06-23T03:13:38Z</dc:date>
    <item>
      <title>When I save a Spark dataframe using df.write.format("csv"), I end up with mulitple csv files. Why is this happening?</title>
      <link>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21582#M14728</link>
      <description />
      <pubDate>Wed, 23 Jun 2021 02:15:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21582#M14728</guid>
      <dc:creator>User16826992666</dc:creator>
      <dc:date>2021-06-23T02:15:48Z</dc:date>
    </item>
    <item>
      <title>Re: When I save a Spark dataframe using df.write.format("csv"), I end up with mulitple csv files. Why is this happening?</title>
      <link>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21583#M14729</link>
      <description>&lt;P&gt;You get multiple files in a folder because spark writes each shuffle partition in-place out to a "part..." file to avoid network I/O. You can use coalesce to bring all the shuffles into a single partition and write it out to a single file but be mindful of the performance implications&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;df.coalesce(1)
   .write.format(csv)
   .option("header", "true")
   .save("singlefile.csv")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jun 2021 03:13:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21583#M14729</guid>
      <dc:creator>aladda</dc:creator>
      <dc:date>2021-06-23T03:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: When I save a Spark dataframe using df.write.format("csv"), I end up with mulitple csv files. Why is this happening?</title>
      <link>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21584#M14730</link>
      <description>&lt;P&gt;Just use &lt;/P&gt;&lt;P&gt;df.coalesce(1).write.csv("File,path")&lt;/P&gt;&lt;P&gt;df.repartition(1).write.csv("file path)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When you are ready to write a DataFrame, first use&amp;nbsp;&lt;A href="https://sparkbyexamples.com/spark/spark-repartition-vs-coalesce/" alt="https://sparkbyexamples.com/spark/spark-repartition-vs-coalesce/" target="_blank"&gt;Spark repartition() and coalesce()&lt;/A&gt;&amp;nbsp;to merge data from all partitions into a single partition and then save it to a file. This still creates a directory and write a single part file inside a directory instead of multiple part files.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Both&amp;nbsp;coalesce() and&amp;nbsp;repartition() are&amp;nbsp;&lt;A href="https://sparkbyexamples.com/apache-spark-rdd/spark-rdd-transformations/" alt="https://sparkbyexamples.com/apache-spark-rdd/spark-rdd-transformations/" target="_blank"&gt;Spark Transformation operations&lt;/A&gt;&amp;nbsp;that shuffle the data from multiple partitions into a single partition. Use coalesce() as it performs better and uses lesser resources compared with repartition().&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note:&amp;nbsp;You have to be very careful when using Spark&amp;nbsp;coalesce() and repartition() methods on larger datasets as they are expensive operations and could throw OutOfMemory errors.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jun 2021 05:54:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21584#M14730</guid>
      <dc:creator>User16826994223</dc:creator>
      <dc:date>2021-06-23T05:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: When I save a Spark dataframe using df.write.format("csv"), I end up with mulitple csv files. Why is this happening?</title>
      <link>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21585#M14731</link>
      <description>&lt;P&gt;This is by design and working as expected. Spark writes the data distributedly. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;use of coalesce (1) can help to generate one file, however this solution is not scalable for large data set as it involves bringing the data to one single task. &lt;/P&gt;</description>
      <pubDate>Wed, 23 Jun 2021 19:12:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/when-i-save-a-spark-dataframe-using-df-write-format-quot-csv/m-p/21585#M14731</guid>
      <dc:creator>brickster_2018</dc:creator>
      <dc:date>2021-06-23T19:12:11Z</dc:date>
    </item>
  </channel>
</rss>

