nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #24

The code block shown below contains an error. The code block is intended to use SQL to return a new DataFrame containing column storeId and column managerName from a table created from DataFrame…

The correct answer is B. The sql() operation should be accessed via the spark variable rather than DataFrame storesDF. sql() is a method on the SparkSession object (accessed via spark), not on a DataFrame object. While storesDF.createOrReplaceTempView('stores') correctly registers the DataFrame as a temporary SQL view, the subsequent SQL query must be executed via spark.sql('SELECT storeId…

Working with DataFrames and Spark SQL

Question

The code block shown below contains an error. The code block is intended to use SQL to return a new DataFrame containing column storeId and column managerName from a table created from DataFrame storesDF. Identify the error. Code block:

storesDF.createOrReplaceTempView("stores") storesDF.sql("SELECT storeId, managerName FROM stores")

Options

  • AThe createOrReplaceTempView() operation does not make a Dataframe accessible via SQL.
  • BThe sql() operation should be accessed via the spark variable rather than DataFrame storesDF.
  • CThere is the sql() operation in DataFrame storesDF. The operation query() should be used
  • DThis cannot be accomplished using SQL - the DataFrame API should be used instead.
  • EThe createOrReplaceTempView() operation should be accessed via the spark variable rather

How the community answered

(18 responses)
  • A
    6% (1)
  • B
    89% (16)
  • D
    6% (1)

Explanation

sql() is a method on the SparkSession object (accessed via spark), not on a DataFrame object. While storesDF.createOrReplaceTempView('stores') correctly registers the DataFrame as a temporary SQL view, the subsequent SQL query must be executed via spark.sql('SELECT storeId, managerName FROM stores'). Calling storesDF.sql(...) is incorrect because DataFrames do not have a sql() method.

Topics

#Spark SQL#DataFrame API#SparkSession#Temporary Views

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice