Exam Certified Associate Developer for Apache Spark All QuestionsBrowse all questions from this exam
Question 142

Which of the following code blocks returns a DataFrame containing a column openDateString, a string representation of Java’s SimpleDateFormat?

Note that column openDate is of type integer and represents a date in the UNIX epoch format — the number of seconds since midnight on January 1st, 1970.

An example of Java's SimpleDateFormat is "Sunday, Dec 4, 2008 1:05 pm".

A sample of storesDF is displayed below:

    Correct Answer: A

    To convert an integer column representing a UNIX epoch time to a formatted date string in Spark, the `from_unixtime` function is used. The function takes two arguments: the column containing the UNIX timestamp and the desired date format. The `from_unixtime` function returns the formatted date as a string. The correct syntax includes using the `withColumn` method to add a new column. Option A uses this method correctly: `storesDF.withColumn(

Discussion
c3d91eeOption: A

A is the right answer # Assuming you have a SparkSession created (replace with your setup if needed) from pyspark.sql import SparkSession from pyspark.sql.functions import from_unixtime # Sample data (replace with your actual DataFrame) data = [("store1", 1668518400), ("store2", 1668432000)] # Sample UNIX epoch timestamps df = spark.createDataFrame(data, ["storeName", "openDate"]) # Convert openDate to formatted string formatted_df = df.withColumn( "openDateString", from_unixtime(col("openDate"), "EEEE, MMM d, yyyy h:mm a") ) # Print the resulting DataFrame formatted_df.show(truncate=False) # Stop the SparkSession (optional) #spark.stop()

azure_bimonsterOption: B

B is likely tru, because A option has some typo issue and the underscore in from keyword is missing before unixtime, and the return type is not specified.

deadbeef38Option: A

new column should be a String, existing column is Int

Sowwy1Option: A

A. storesDF.withColumn("openDatestring", from unixtime(col("openDate“), “EEEE, MMM d, yyyy h:mm a"))