Support Questions
Find answers, ask questions, and share your expertise
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

How to concatenate date to file in pyspark


How to concatenate date to file in pyspark

New Contributor

Hello community, I have created the following pyspark query:

from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('ops').getOrCreate()
df ='/home/packt/Downloads/Spark_DataFrames/HumanResources_vEmployeeDepartment.csv',inferSchema=True,header=True)
counts = spark.sql("""SELECT
FROM HumanResources_vEmployeeDepartment
ORDER BY FirstName, LastName DESC""")

I would like to add the current date and time to the file called myresults3. I think the code would look something like the following:


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. Any help will be appreciated.

Cheers Carlton


Re: How to concatenate date to file in pyspark

@Carlton Patterson

You can use the python's datetime package to obtain the current date.

import datetime 
currentdate ="%Y-%m-%d") 
print currentdate 


And then use the currentdate in output file name.

counts.coalesce(1).write.csvCONCAT("/home/packt/Downloads/myresults3-" + currentdate + ".csv")

Hope this helps.

P.S. If you want date and time use:"%Y-%m-%d %H:%M")

Re: How to concatenate date to file in pyspark

As this is duplicate, Please delete/close this question.