Databricks Data Engineer Associate exam dumps

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

Select 3

You are working with a timestamp column named event_time in a Delta table. You want to extract the year and month as separate columns from this timestamp in a PySpark DataFrame. Which of the following code snippets would achieve this?

  1. A

    df = df.withColumn('year', year(df['event_time'])).withColumn('month', month(df['event_time']))

  2. B

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

  3. C

    df = df.selectExpr('year(event_time) as year', 'month(event_time) as month')

  4. D

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

  5. E

    df = df.withColumn('year', F.date_format('event_time', 'yyyy')).withColumn('month', F.date_format('event_time', 'MM'))

Show answer and explanation

Correct answers: B, C, E

Explanation

In PySpark, calendar data like year and month can be extracted using functions like year, month, and date_format from pyspark.sql.functions. Additionally, SQL expressions can be used with selectExpr to achieve the same result. However, there is no extract function for this purpose in PySpark. Using the correct methods ensures accurate data processing and compatibility with Spark APIs.

  • A. Incorrect.

    This option is incorrect because year and month functions cannot be directly called without importing them or referencing them from the pyspark.sql.functions module.

  • B. Correct.

    This option is correct because it uses the year and month functions from pyspark.sql.functions, which are the recommended functions to extract calendar data from a timestamp.

  • C. Correct.

    This option is correct because selectExpr allows SQL expressions like year(event_time) and month(event_time) to extract the year and month from a timestamp column.

  • D. Incorrect.

    This option is incorrect because there is no extract function in PySpark for extracting specific calendar fields from a timestamp.

  • E. Correct.

    This option is correct because date_format from pyspark.sql.functions can be used to extract specific parts of a timestamp as strings, such as the year ('yyyy') and month ('MM').

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