nerdexam
Microsoft

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.

Implement and manage a data analytics environment

Question

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, ('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)
  • A
    4% (2)
  • B
    92% (44)
  • C
    2% (1)
  • D
    2% (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.

A%%pyspark

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.

B%pysparkCorrect

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.

C%%spark

`%%spark` is used to specify that a cell should be executed using the Spark (Scala) language, not PySpark.

D%%sql

`%%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

#Synapse Notebooks#PySpark#Magic Commands#Spark Environment

Community Discussion

No community discussion yet for this question.

Full DP-500 Practice