Databricks Data Engineer Associate exam dumps

Databricks Data Engineer Associate practice question 158 of 532

Databricks Certified Data Engineer Associate. Associate level, Databricks. Free question with the correct answer and a full explanation.

Databricks Data Engineer Associate Question 158

Select 2

You are working with a DataFrame in Databricks that contains a column named 'event_time' with timestamp values. You need to extract the year, month, and day as separate columns from this timestamp. Which of the following options correctly retrieves this information?

  1. A

    df.withColumn('year', year(col('event_time'))).withColumn('month', month(col('event_time'))).withColumn('day', dayofmonth(col('event_time')))

  2. B

    df.withColumn('year', extract_year(col('event_time'))).withColumn('month', extract_month(col('event_time'))).withColumn('day', extract_day(col('event_time')))

  3. C

    df.select(year(col('event_time')).alias('year'), month(col('event_time')).alias('month'), dayofmonth(col('event_time')).alias('day'))

  4. D

    df.withColumn('year', col('event_time').year()).withColumn('month', col('event_time').month()).withColumn('day', col('event_time').day())

  5. E

    df.withColumn('year', col('event_time').getYear()).withColumn('month', col('event_time').getMonth()).withColumn('day', col('event_time').getDay())

Show answer and explanation

Correct answers: A, C

Explanation

To extract calendar data (year, month, day) from a timestamp column in a Spark DataFrame, you can use the Spark SQL functions year, month, and dayofmonth. These functions can be used with withColumn to add new columns or with select to directly retrieve the extracted values with aliases. Other methods or function names are invalid in this context.

  • A. Correct.

    Correct. This uses the Spark SQL functions year, month, and dayofmonth, which are the standard methods to extract calendar data from timestamps in Spark.

  • B. Incorrect.

    Incorrect. extract_year, extract_month, and extract_day are not valid Spark SQL functions. The correct functions are year, month, and dayofmonth.

  • C. Correct.

    Correct. This approach also uses the Spark SQL functions year, month, and dayofmonth but retrieves the extracted values by selecting them directly with aliases.

  • D. Incorrect.

    Incorrect. While col is valid, the functions .year(), .month(), and .day() are not valid methods for extracting calendar data in PySpark.

  • E. Incorrect.

    Incorrect. The methods .getYear(), .getMonth(), and .getDay() are not supported for extracting calendar data from Spark DataFrame columns.

Timed practice exam

Take a Databricks Data Engineer Associate practice test under exam conditions

45 questions in 90 minutes, drawn from this bank, with a score report and a per-question review when you finish.

Start timed exam