DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #113
Which of the following code blocks uses SQL to return a new DataFrame containing column storeId and column managerName from a table created from DataFrame storesDF?
The correct answer is D. storesDF.createOrReplaceTempView("stores"). To query a DataFrame using Spark SQL, you must first register it as a temporary view using createOrReplaceTempView('viewName'), then call spark.sql('SELECT ...'). Choice D - storesDF.createOrReplaceTempView('stores') - correctly registers the DataFrame as a temp view named…
Question
Which of the following code blocks uses SQL to return a new DataFrame containing column storeId and column managerName from a table created from DataFrame storesDF?
Options
- AstoresDF.createOrReplaceTempView()
- BstoresDF.query("SELECT storeid, managerName from stores")
- Cspark.createOrReplaceTempView("storesDF")
- DstoresDF.createOrReplaceTempView("stores")
- EstoresDF.createOrReplaceTempView("stores")
How the community answered
(22 responses)- C5% (1)
- D86% (19)
- E9% (2)
Explanation
To query a DataFrame using Spark SQL, you must first register it as a temporary view using createOrReplaceTempView('viewName'), then call spark.sql('SELECT ...'). Choice D - storesDF.createOrReplaceTempView('stores') - correctly registers the DataFrame as a temp view named stores, which can then be queried with spark.sql('SELECT storeId, managerName FROM stores'). Choice A is missing the required view name argument. Choice B uses a non-existent .query() method. Choice C incorrectly calls createOrReplaceTempView on the spark session object rather than on the DataFrame - only DataFrames have this method.
Topics
Community Discussion
No community discussion yet for this question.