DP-500 · Question #137
You have the following code in a cell in an Azure Synapse notebook. insert command here new_rows = [('WA',2,45000), ('WA',35,65000),('WA',50,85000)] demo_df = spark.createDataFrame(new_rows…
The correct answer is B. %pyspark. To execute PySpark code within an Azure Synapse notebook when the default language is not PySpark, the magic command %pyspark should be used as the first line of the cell to specify the language for that entire cell.
Question
insert command here
new_rows = [('WA',2,45000), ('WA',35,65000),('WA',50,85000)] demo_df = spark.createDataFrame(new_rows, ('state', 'age', 'salary')) demo_df.createOrReplaceTempView('demo_df') You need to replace the comment on the first line of the code with the correct magic command. Which magic command should you use?Options
- A%%pyspark
- B%pyspark
- C%%spark
- D%%sql
How the community answered
(48 responses)- A4% (2)
- B92% (44)
- C2% (1)
- D2% (1)
Why each option
To execute PySpark code within an Azure Synapse notebook when the default language is not PySpark, the magic command `%pyspark` should be used as the first line of the cell to specify the language for that entire cell.
While `%%pyspark` is a common cell magic command in Jupyter-like environments to specify PySpark, for the specific context of Azure Synapse notebooks when placed as the first line to declare the cell's language, `%pyspark` is the expected magic command as per the correct answer.
In Azure Synapse notebooks, the `%pyspark` magic command can be used as the first line of a cell to specify that the entire cell's content should be executed as PySpark code, overriding the notebook's default language. This allows the subsequent PySpark code, such as `spark.createDataFrame`, to run correctly within that cell.
`%%spark` is used to specify that a cell should be executed using the Spark (Scala) language, not PySpark.
`%%sql` is used to specify that a cell should be executed using SQL, not PySpark.
Concept tested: Azure Synapse Notebook magic commands
Source: https://learn.microsoft.com/en-us/azure/synapse-analytics/spark/apache-spark-notebook-features#notebook-language
Topics
Community Discussion
No community discussion yet for this question.