Databricks Data Engineer Associate Question 51
Select 3You are working on a Databricks notebook and need to process data using both Python and SQL within the same notebook. Which of the following steps or techniques can you use to work with multiple languages in the same notebook?
- A
Use magic commands like
%pythonor%sqlto specify the language of a cell. - B
Write all code in the same cell and specify the language using inline comments (e.g.,
#pythonor--sql). - C
Use the default language of the notebook for one part of the code and use magic commands for cells in a different language.
- D
Switch the notebook's default language to the required language before every cell.
- E
Pass variables between languages using a shared Spark session or global temporary views.
Show answer and explanation
Correct answers: A, C, E
Explanation
Databricks notebooks support working with multiple languages by using magic commands (e.g., %python, %sql, %r, %scala). The default language of the notebook applies unless overridden by these commands. To pass data between languages, you can use shared Spark objects like DataFrames or create global temporary views for use in SQL queries.
- A. Correct.
Correct: Magic commands like
%pythonor%sqlare used in Databricks notebooks to specify the language of a cell. This is a standard way to work with multiple languages in a notebook. - B. Incorrect.
Incorrect: Inline comments like
#pythonor--sqlare not used to specify cell languages. This is not supported in Databricks notebooks. - C. Correct.
Correct: The default language of the notebook applies to all cells unless overridden by a magic command. You can use the default language for part of the notebook and magic commands for other cells in different languages.
- D. Incorrect.
Incorrect: Switching the notebook's default language before every cell is not practical or necessary. The default language is typically set once and magic commands are used for other languages.
- E. Correct.
Correct: Variables and data can be shared between languages using the Spark session (e.g., Spark DataFrames) or by creating global temporary views, which can be queried in SQL cells.