QuestionQ239

VerifiedTroubleshooting and Tuning Apache Spark DataFrame API Applications.

The code block below has an error. It is intended to return a DataFrame with an openDateString column that contains a string representation in Java’s SimpleDateFormat. Identify the error.

Column openDate has type integer and represents a UNIX epoch date—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 appears below:

Question Image

Code block:

storesDF.withColumn("openDateString", from_unixtime(col("openDate"), "EEE, MMM d, yyyy h:mm a", TimestampType()))  
Explanation

from_unixtime converts epoch seconds to a formatted string and accepts the epoch-seconds expression plus an optional format string. The format EEE, MMM d, yyyy h:mm a is valid for the intended output, but TimestampType() is an invalid third argument. Integer epoch-seconds do not need to be converted to a timestamp or long first. PySpark from_unixtime documentation

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!