Databricks Data Engineer Associate exam dumps

Databricks Data Engineer Associate practice question 155 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 155

Select 2

You are working with a DataFrame in Databricks containing a column named event_time with timestamp values. You need to extract the year and month from this timestamp column to perform grouping operations. Which of the following options demonstrate correct methods to achieve this in PySpark?

  1. A

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

  2. B

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

  3. C

    df = df.withColumn('year', extract('year', col('event_time'))).withColumn('month', extract('month', col('event_time')))

  4. D

    df = df.withColumn('year', date_format(col('event_time'), 'y')).withColumn('month', date_format(col('event_time'), 'MM'))

  5. E

    df = df.withColumn('year', col('event_time').substr(1, 4)).withColumn('month', col('event_time').substr(6, 2))

Show answer and explanation

Correct answers: A, D

Explanation

Extracting calendar data from a timestamp column in PySpark can be effectively done using either the year and month functions or the date_format function with appropriate format strings. These methods are optimized for working with timestamp types and ensure accuracy and clarity. Other methods, such as using attributes, unsupported functions, or substring extraction, are either invalid or not recommended in professional scenarios.

  • A. Correct.

    This is correct. The year and month functions in PySpark are explicitly designed to extract the respective calendar components from a timestamp column.

  • B. Incorrect.

    This is incorrect. There is no direct year or month attribute on the column object in PySpark; attempting to use these attributes will result in an error.

  • C. Incorrect.

    This is incorrect. PySpark does not have an extract function for this purpose. Using this will result in a syntax error.

  • D. Correct.

    This is correct. The date_format function can extract specific parts of a timestamp column by specifying the desired format (e.g., 'y' for year, 'MM' for month).

  • E. Incorrect.

    This is incorrect. While the substr function can technically retrieve parts of a string, it is not a reliable or recommended approach for extracting calendar data from a timestamp column, as timestamps are not guaranteed to follow a fixed format.

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