Databricks Data Engineer Associate Question 58
Single answerYou are working on a Databricks notebook that processes data and want to reuse a function written in another notebook called 'utilities'. How can you run this 'utilities' notebook from your current notebook to access its functionality?
- A
Use the %run magic command followed by the relative path to the 'utilities' notebook.
- B
Use the %sql magic command followed by the relative path to the 'utilities' notebook.
- C
Use the dbutils.notebook.run() function to call the 'utilities' notebook.
- D
Use the import keyword in Python to directly import the 'utilities' notebook as a module.
Show answer and explanation
Correct answer: A
Explanation
To run one notebook from within another notebook in Databricks and access its functions or variables in the current context, the %run magic command should be used. This allows the current notebook to execute the code from the specified notebook and inherit its scope. Other methods like dbutils.notebook.run() are used for separate execution flows or job workflows, not inline execution.
- A. Correct.
Correct. The %run magic command is used to execute another notebook within the current notebook. You must specify the relative path to the notebook, ensuring proper access to its variables and functions.
- B. Incorrect.
Incorrect. The %sql magic command is used for running SQL queries directly in a Databricks notebook, not for executing other notebooks.
- C. Incorrect.
Incorrect. The dbutils.notebook.run() function is used to trigger a notebook as a separate job with arguments and captures its return value, but it does not directly execute the notebook in the current context.
- D. Incorrect.
Incorrect. The import keyword in Python cannot directly import a Databricks notebook as a module, as Databricks notebooks are not treated as standard Python modules.