DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #86
Which of the following code blocks reads a CSV at the file path filePath into a Data Frame with the specified schema schema?
The correct answer is C. spark.read.schema(schema).csv(filePath). Option C is correct because in PySpark, spark.read is a property (a DataFrameReader object), not a method - so it takes no parentheses. The .schema() method accepts a StructType variable (here schema), not a string, followed by .csv(filePath) to specify the source format and…
Question
Which of the following code blocks reads a CSV at the file path filePath into a Data Frame with the specified schema schema?
Options
- Aspark.read().csv(filePath)
- Bspark.read().schema("schema").csv(filePath)
- Cspark.read.schema(schema).csv(filePath)
- Dspark.read.schema("schema").csv(filePath)
- Espark.read().schema(schema).csv(filePath)
How the community answered
(47 responses)- B2% (1)
- C94% (44)
- E4% (2)
Explanation
Option C is correct because in PySpark, spark.read is a property (a DataFrameReader object), not a method - so it takes no parentheses. The .schema() method accepts a StructType variable (here schema), not a string, followed by .csv(filePath) to specify the source format and path.
Why the distractors fail:
- A & B & E:
spark.read()with parentheses is aTypeError-readis a property, not callable. - B & D: Passing
"schema"(a string literal) to.schema()instead of the actualStructTypevariableschemawill either fail or be misinterpreted. - A: Also missing
.schema()entirely, so no schema is applied at all.
Memory tip: Think "read is free, schema needs the variable" - spark.read has no (), and .schema() wants the real object, not its name in quotes.
Topics
Community Discussion
No community discussion yet for this question.