<?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 How to concatenate a date to a filename in pyspark in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219978#M181863</link>
    <description>&lt;P&gt;Hello community,&lt;/P&gt;&lt;P&gt;I have created the following pyspark query:&lt;/P&gt;&lt;PRE&gt;from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('ops').getOrCreate()
df = spark.read.csv('/home/packt/Downloads/Spark_DataFrames/HumanResources_vEmployeeDepartment.csv',inferSchema=True,header=True)
df.createOrReplaceTempView('HumanResources_vEmployeeDepartment')
counts = spark.sql("""SELECT
FirstName
,LastName
,JobTitle
FROM HumanResources_vEmployeeDepartment
ORDER BY FirstName, LastName DESC""")
counts.coalesce(1).write.csv("/home/packt/Downloads/myresults3.csv") &lt;/PRE&gt;&lt;P&gt;I would like to add the current date and time to the file called myresults3.&lt;/P&gt;&lt;P&gt;I think the code would look something like the following:&lt;/P&gt;&lt;PRE&gt;counts.coalesce(1).write.csvCONCAT("/home/packt/Downloads/'myresults3'-CURRENTDATE.csv") &lt;/PRE&gt;&lt;P&gt;I'm sure I'm way off the mark with the above attempt, but I'm sure you can see what I'm trying to achieve.&lt;/P&gt;&lt;P&gt;Any help will be appreciated.&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;Carlton&lt;/P&gt;</description>
    <pubDate>Mon, 13 Aug 2018 16:42:44 GMT</pubDate>
    <dc:creator>barlow</dc:creator>
    <dc:date>2018-08-13T16:42:44Z</dc:date>
    <item>
      <title>How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219978#M181863</link>
      <description>&lt;P&gt;Hello community,&lt;/P&gt;&lt;P&gt;I have created the following pyspark query:&lt;/P&gt;&lt;PRE&gt;from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('ops').getOrCreate()
df = spark.read.csv('/home/packt/Downloads/Spark_DataFrames/HumanResources_vEmployeeDepartment.csv',inferSchema=True,header=True)
df.createOrReplaceTempView('HumanResources_vEmployeeDepartment')
counts = spark.sql("""SELECT
FirstName
,LastName
,JobTitle
FROM HumanResources_vEmployeeDepartment
ORDER BY FirstName, LastName DESC""")
counts.coalesce(1).write.csv("/home/packt/Downloads/myresults3.csv") &lt;/PRE&gt;&lt;P&gt;I would like to add the current date and time to the file called myresults3.&lt;/P&gt;&lt;P&gt;I think the code would look something like the following:&lt;/P&gt;&lt;PRE&gt;counts.coalesce(1).write.csvCONCAT("/home/packt/Downloads/'myresults3'-CURRENTDATE.csv") &lt;/PRE&gt;&lt;P&gt;I'm sure I'm way off the mark with the above attempt, but I'm sure you can see what I'm trying to achieve.&lt;/P&gt;&lt;P&gt;Any help will be appreciated.&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;Carlton&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 16:42:44 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219978#M181863</guid>
      <dc:creator>barlow</dc:creator>
      <dc:date>2018-08-13T16:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219979#M181864</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;  You can use the python's datetime package to obtain the current date. &lt;/P&gt;&lt;P&gt;import datetime&lt;/P&gt;&lt;P&gt;
currentdate = datetime.datetime.now().strftime("%Y-%m-%d") &lt;/P&gt;&lt;P&gt;print currentdate &lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; 2018-08-13&lt;/P&gt;&lt;P&gt;And then use the currentdate in output file name. &lt;/P&gt;&lt;PRE&gt;counts.coalesce(1).write.csv("/home/packt/Downloads/myresults3-" + currentdate + ".csv") &lt;/PRE&gt;&lt;P&gt;Hope this helps. &lt;/P&gt;&lt;P&gt;P.S. If you want date and time use: datetime.datetime.now().strftime("%Y-%m-%d %H:%M")&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 17:44:53 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219979#M181864</guid>
      <dc:creator>sandyy006</dc:creator>
      <dc:date>2018-08-13T17:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219980#M181865</link>
      <description>&lt;P&gt;Looks like there are 3 questions with same description...&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 17:45:45 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219980#M181865</guid>
      <dc:creator>sandyy006</dc:creator>
      <dc:date>2018-08-13T17:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219981#M181866</link>
      <description>&lt;P&gt;Sandeep,&lt;/P&gt;&lt;P&gt;Thanks for reaching out.&lt;/P&gt;&lt;P&gt;I'm getting the following error from the import function&lt;/P&gt;Append ResultsClear Results&lt;P&gt;&lt;CODE&gt;  File "&amp;lt;ipython-input-7-3dab170099f6&amp;gt;", line 3    import datetime currentdate = datetime.datetime.now().strftime("%Y-%m-%d")                              ^SyntaxError: invalid syntax&lt;/CODE&gt; &lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 18:04:22 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219981#M181866</guid>
      <dc:creator>barlow</dc:creator>
      <dc:date>2018-08-13T18:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219982#M181867</link>
      <description>&lt;P&gt;The syntax error is with 'currentdate'&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 18:05:07 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219982#M181867</guid>
      <dc:creator>barlow</dc:creator>
      <dc:date>2018-08-13T18:05:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219983#M181868</link>
      <description>&lt;P&gt;Looks like some issue with text formatting.  Try this: &lt;/P&gt;&lt;P&gt;import datetime &lt;/P&gt;&lt;P&gt;currentdate = datetime.datetime.now().strftime("%Y-%m-%d") &lt;/P&gt;&lt;P&gt;print currentdate &lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; 2018-08-13&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 18:11:49 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219983#M181868</guid>
      <dc:creator>sandyy006</dc:creator>
      <dc:date>2018-08-13T18:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219984#M181869</link>
      <description>&lt;P&gt;I now get the following error:&lt;/P&gt;&lt;P&gt;File "&amp;lt;ipython-input-13-588f4561c3f0&amp;gt;", line 7    print currentdate()                    ^SyntaxError: invalid syntax&lt;/P&gt;&lt;P&gt;The invalid syntax is currentdate()&lt;/P&gt;&lt;P&gt;Without the parentheses I get the following error:&lt;/P&gt;&lt;P&gt;File "&amp;lt;ipython-input-14-8d268659919b&amp;gt;", line 1    print currentdate                    ^SyntaxError: Missing parentheses in call to 'print'&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 18:20:47 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219984#M181869</guid>
      <dc:creator>barlow</dc:creator>
      <dc:date>2018-08-13T18:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219985#M181870</link>
      <description>&lt;P&gt;What is the python version you are using? if its python 3.x &lt;/P&gt;&lt;P&gt;Use: print(currentdate) &lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 18:26:34 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219985#M181870</guid>
      <dc:creator>sandyy006</dc:creator>
      <dc:date>2018-08-13T18:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219986#M181871</link>
      <description>&lt;P&gt;I'm using python version 3 and print(currentate) worked. Thanks. However, when I run the full query I get the following error:&lt;/P&gt;&lt;PRE&gt;ipython-input-22-8c743396e037&amp;gt; in &amp;lt;module&amp;gt;()     18FROMHumanResources_vEmployeeDepartment     19 ORDER BY FirstName, LastName DESC""")
---&amp;gt; 20counts.coalesce(1).write.csvCONCAT("/home/packt/Downloads/myresults7-"+currentdate+".csv")     
'DataFrameWriter' object has no attribute 'csvCONCAT'&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Aug 2018 19:08:10 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219986#M181871</guid>
      <dc:creator>barlow</dc:creator>
      <dc:date>2018-08-13T19:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219987#M181872</link>
      <description>&lt;P&gt;Hi Sandeep,&lt;/P&gt;&lt;P&gt;I should be clear about what I'm trying to achieve.&lt;/P&gt;&lt;P&gt;I would like the output to include only the delta change. I thought that having the current date would be sufficient, but I just realized that having just the currentdate won't let me know if there has been a change to the data. &lt;/P&gt;&lt;P&gt;Therefore, while your helping me could you also help me figure out how to include the currentdate and the delta change in data?&lt;/P&gt;&lt;P&gt;Much appreciated.&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 19:19:21 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219987#M181872</guid>
      <dc:creator>barlow</dc:creator>
      <dc:date>2018-08-13T19:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219988#M181873</link>
      <description>&lt;A rel="user" href="https://community.cloudera.com/users/63166/carlton.html" nodeid="63166"&gt;@Carlton Patterson&lt;/A&gt;&lt;P&gt;You can use "&lt;EM&gt;mode("append")&lt;/EM&gt;" to append the new data to existing one.&lt;/P&gt;&lt;PRE&gt;counts.coalesce(1).write.mode("append").csv("/home/packt/Downloads/myresults7-"+currentdate+".csv")     &lt;/PRE&gt;&lt;P&gt;P.S please use 'reply' on this comment instead of writing a new comment. In this way we can maintain the conversaion in order. &lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 20:43:51 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219988#M181873</guid>
      <dc:creator>sandyy006</dc:creator>
      <dc:date>2018-08-13T20:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219989#M181874</link>
      <description>&lt;P&gt;Hi Sandeep, thanks. It works very well. Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 23:11:58 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219989#M181874</guid>
      <dc:creator>barlow</dc:creator>
      <dc:date>2018-08-13T23:11:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219990#M181875</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; Glad it helped, Do click on 'Accept' on my answer and mark this thread as closed. &lt;/P&gt;</description>
      <pubDate>Mon, 13 Aug 2018 23:14:52 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219990#M181875</guid>
      <dc:creator>sandyy006</dc:creator>
      <dc:date>2018-08-13T23:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to concatenate a date to a filename in pyspark</title>
      <link>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219991#M181876</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; Looks like you have accepted another comment. I've made this reply as comment and this should be the correct one to accept as it helped in resolving your issue. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2018 02:26:18 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/How-to-concatenate-a-date-to-a-filename-in-pyspark/m-p/219991#M181876</guid>
      <dc:creator>sandyy006</dc:creator>
      <dc:date>2018-08-14T02:26:18Z</dc:date>
    </item>
  </channel>
</rss>

