Databricks Certified Associate Developer for Apache Spark — Question 155
The code block shown below contains an error. The code block is intended to read JSON at the file path filePath into a DataFrame with the specified schema schema. Identify the error.
Code block:
spark.read.schema("schema").format("json").load(filePath)
Answer options
- A. The schema operation from read takes a schema object rather than a string — the argument should be schema.
- B. There is no load() operation for DataFrameReader — it should be replaced with the json() operation.
- C. The spark.read operation should be followed by parentheses in order to return a DataFrameReader object.
- D. There is no read property of spark — spark should be replaced with DataFrame.
- E. The schema operation from read takes a column rather than a string — the argument should be col("schema").
Correct answer: A
Explanation
The correct answer is A because the schema method expects a schema object, not a string representing the schema. The other options are incorrect as load() is a valid method for DataFrameReader, spark.read does not require parentheses to return a DataFrameReader, spark is indeed a valid property, and schema does not take a column as an argument.