Databricks Data Engineer Associate Question 201
Single answerYou are working on a Databricks notebook and need to use a custom Python function named transform_data. The function is defined in another file located in a shared workspace repository under the folder path /Workspace/Repos/shared-project/functions/. How would you correctly identify the location of this function and make it usable in your current notebook?
- A
Use
%run /Workspace/Repos/shared-project/functions/transform_datato import the function. - B
Use
import /Workspace/Repos/shared-project/functions/transform_datato import the function. - C
Use
%run /Workspace/Repos/shared-project/functionsto import all functions in the folder, includingtransform_data. - D
Use
from /Workspace/Repos/shared-project/functions import transform_datato import the function.
Show answer and explanation
Correct answer: A
Explanation
In Databricks, the %run command is used to execute code from another notebook or file so that its contents, including functions, variables, and classes, are accessible in the current notebook. To make the transform_data function usable, you need to specify the exact file path using %run, such as %run /Workspace/Repos/shared-project/functions/transform_data. Other options like import or from ... import are not compatible with workspace paths in Databricks.
- A. Correct.
This is the correct way to identify and execute a function defined in another file located in the Databricks workspace.
%runexecutes all the code in the referenced file, making thetransform_datafunction accessible in your notebook. - B. Incorrect.
importcannot be directly used with workspace paths as it is not a valid method for identifying and importing functions in Databricks. - C. Incorrect.
%runwith only the folder path will not import thetransform_datafunction specifically. Instead, you need to specify the exact file path to successfully execute the function. - D. Incorrect.
fromwith the workspace path is not a valid way to import functions from files in Databricks, as it does not recognize workspace paths using this syntax.