
Explanation:
Correct Answer: C — spark.read.schema(schema).csv(filePath)
In PySpark, spark.read is an attribute, not a method, so no parentheses are used.
.schema(schema) expects a StructType object, not a string.
.csv(filePath) tells Spark to read the file from the given path.
This chain correctly applies the schema when reading the CSV.
In PySpark, spark.read is an attribute, not a method, so no parentheses are used.
.schema(schema) expects a StructType object, not a string.
.csv(filePath) tells Spark to read the file from the given path.
This chain correctly applies the schema when reading the CSV.
Why Other Options Are Wrong in PySpark
A: spark.read().csv(filePath) → Missing the .schema(schema) part → schema won't be enforced.
B/D: .schema("schema") → Passing "schema" as a string is invalid; must be a StructType.
E: spark.read().schema(schema).csv(filePath) → This uses parentheses after spark.read, which works in Scala but fails in PySpark (AttributeError).
A: spark.read().csv(filePath) → Missing the .schema(schema) part → schema won't be enforced.
B/D: .schema("schema") → Passing "schema" as a string is invalid; must be a StructType.
E: spark.read().schema(schema).csv(filePath) → This uses parentheses after spark.read, which works in Scala but fails in PySpark (AttributeError).
Ultimate access to all questions.
Which of the following code blocks correctly reads a CSV file from the specified path filePath into a DataFrame using the given schema schema?
A
spark.read().csv(filePath)
B
spark.read().schema(“schema”).csv(filePath)
C
spark.read.schema(schema).csv(filePath)
D
spark.read.schema(“schema”).csv(filePath)
E
spark.read().schema(schema).csv(filePath)
No comments yet.