Databricks Data Engineer Associate exam dumps

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

Single answer

You are working with a DataFrame in Databricks that contains a column named 'log_data' with entries like 'userID:12345, action:login, status:success'. You need to extract only the numeric user ID from this column into a new column. Which of the following code snippets correctly accomplish this task?

  1. A

    df = df.withColumn('user_id', F.regexp_extract(F.col('log_data'), 'userID:(\d+)', 1))

  2. B

    df = df.withColumn('user_id', F.extract(F.col('log_data'), 'userID:(\d+)', 1))

  3. C

    df = df.withColumn('user_id', F.substring(F.col('log_data'), 'userID:(\d+)', 1))

  4. D

    df = df.withColumn('user_id', F.regexp_replace(F.col('log_data'), 'userID:(\d+)', 1))

Show answer and explanation

Correct answer: A

Explanation

To extract a specific pattern from a string column in PySpark, the regexp_extract function should be used. It takes three arguments: the column to apply the regex on, the regex pattern, and the group index to extract. In this case, the pattern 'userID:(\d+)' matches 'userID:' followed by one or more digits, and the group index 1 extracts the numeric user ID.

  • A. Correct.

    This is the correct answer. The regexp_extract function is used to extract a specific pattern from a string column, and the provided regex 'userID:(\d+)' captures the numeric user ID.

  • B. Incorrect.

    This is incorrect. The extract function does not exist in PySpark. The correct function to use is regexp_extract.

  • C. Incorrect.

    This is incorrect. The substring function is used to extract a substring based on position, not a pattern. It cannot handle regular expressions.

  • D. Incorrect.

    This is incorrect. The regexp_replace function is used to replace a pattern in a string with another value, not to extract a specific pattern.

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