Databricks Data Engineer Associate Question 200
Single answerYou are working on a Databricks notebook and need to reuse a function named process_data. The function has already been defined in a separate Python file stored in the workspace at /Workspace/Shared/Utilities/functions.py. How can you correctly locate and use the function in your notebook?
- A
Use the
%run /Workspace/Shared/Utilities/functions.pycommand in a notebook cell to load the file and access the function. - B
Directly call
process_data()in the notebook without any additional steps since the function is stored in the workspace. - C
Use the
importstatement:import /Workspace/Shared/Utilities/functionsto import the function from the file. - D
Create a new Python file in the same directory as your notebook and copy-paste the
process_datafunction into it.
Show answer and explanation
Correct answer: A
Explanation
To reuse a function defined in a separate Python file stored in the Databricks workspace, the %run command is the most efficient and appropriate method. It executes the code in the specified file, making the function available in the current notebook session. Directly calling the function, using an improper import statement, or copying the code unnecessarily are either incorrect or inefficient approaches.
- A. Correct.
This is the correct approach. The
%runcommand allows you to execute the code from the specified file in your notebook, making theprocess_datafunction available for use. - B. Incorrect.
This is incorrect. Functions stored in workspace files are not automatically available in notebooks without explicitly loading or importing them.
- C. Incorrect.
This is incorrect. The
importstatement cannot directly reference workspace paths. You would need to configure the workspace file as a module for this to work, which is not a standard approach in Databricks. - D. Incorrect.
This is incorrect. While this approach could work, it is inefficient and does not leverage the reusable nature of the function already stored in the workspace.