<?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>question Re: Unable to Create a single file with PySpark query in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Unable-to-Create-a-single-file-with-PySpark-query/m-p/213877#M175801</link>
    <description>&lt;P&gt;&lt;A rel="user" href="https://community.cloudera.com/users/63166/carlton.html" nodeid="63166"&gt;@Carlton Patterson&lt;/A&gt;
&lt;/P&gt;&lt;P&gt;This is &lt;STRONG&gt;not possible with default save/csv/json&lt;/STRONG&gt; functions but using &lt;A href="https://stackoverflow.com/questions/40792434/spark-dataframe-save-in-single-file-on-hdfs-location" target="_blank"&gt;Hadoop API&lt;/A&gt;&lt;STRONG&gt;&lt;/STRONG&gt; we can rename the filename.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Example:&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&amp;gt;&amp;gt;&amp;gt; df=spark.sql("select int(1)id,string('ll')name") //create a dataframe
&amp;gt;&amp;gt;&amp;gt; df.coalesce(1).write.mode("overwrite").csv("/user/shu/test/temp_dir") //writing the df to temp-dir
&amp;gt;&amp;gt;&amp;gt; from py4j.java_gateway import java_import
&amp;gt;&amp;gt;&amp;gt; java_import(spark._jvm, 'org.apache.hadoop.fs.Path')
&amp;gt;&amp;gt;&amp;gt; fs = spark._jvm.org.apache.hadoop.fs.FileSystem.get(spark._jsc.hadoopConfiguration())
&amp;gt;&amp;gt;&amp;gt; file = fs.globStatus(sc._jvm.Path('/user/shu/test/temp_dir/part*'))[0].getPath().getName() 	//get 	the filename of temp_dir
&amp;gt;&amp;gt;&amp;gt; fs.rename(sc._jvm.Path('/user/shu/test/temp_dir/' + file),sc._jvm.Path('/user/shu/test/mydata.csv')) //rename the temp directory file with desired filename and directory path
&amp;gt;&amp;gt;&amp;gt; fs.delete(sc._jvm.Path('/user/shu/test/temp_dir'), True) //delete the temp directory.&lt;/PRE&gt;&lt;P&gt;-&lt;/P&gt;&lt;P&gt;If the Answer helped to resolve your issue, &lt;STRONG&gt;Click on Accept button below to accept the answer,&lt;/STRONG&gt; That would be great help to Community users to find solution quickly for these kind of issues.&lt;/P&gt;</description>
    <pubDate>Sat, 20 Oct 2018 23:34:33 GMT</pubDate>
    <dc:creator>Shu_ashu</dc:creator>
    <dc:date>2018-10-20T23:34:33Z</dc:date>
  </channel>
</rss>

