DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #129
The code block shown below contains an error. The code block is intended to print the schema of DataFrame storesDF. Identify the error. Code block: storesDF.printSchema.getAs[String]
The correct answer is D. The printSchema member of DataFrame is an operation prints the DataFrame - there is no need. printSchema() is a complete, self-contained operation that prints the DataFrame schema to the console and returns Unit - chaining getAs[String] on its result is meaningless and is the error.
Question
The code block shown below contains an error. The code block is intended to print the schema of DataFrame storesDF. Identify the error. Code block:
storesDF.printSchema.getAs[String]
Options
- AThere is no printSchema member of DataFrame - the getSchema() operation should be used
- BThere is no printSchema member of DataFrame - the schema() operation should be used instead.
- CThe entire line needs to be a string - it should be wrapped by str().
- DThe printSchema member of DataFrame is an operation prints the DataFrame - there is no need
- EThere is no printSchema member of DataFrame - schema and the print() function should be used
How the community answered
(26 responses)- A4% (1)
- B4% (1)
- D92% (24)
Why each option
printSchema() is a complete, self-contained operation that prints the DataFrame schema to the console and returns Unit - chaining getAs[String] on its result is meaningless and is the error.
printSchema is a valid member of DataFrame - getSchema() does not exist as a replacement.
printSchema is a valid member of DataFrame - schema is a separate property that returns a StructType object rather than printing it.
Wrapping the line in str() is not valid Scala and is unrelated to the actual error.
printSchema() is defined on DataFrame as a Unit-returning method that directly prints the schema tree to standard output. Because it returns Unit (void), there is no value to chain getAs[String] onto. The fix is simply to call storesDF.printSchema() with nothing appended afterward.
printSchema is a valid member of DataFrame - using schema with print() is an alternative approach but not the fix to the error described.
Concept tested: DataFrame printSchema method and return type
Source: https://spark.apache.org/docs/latest/api/scala/org/apache/spark/sql/Dataset.html
Topics
Community Discussion
No community discussion yet for this question.