DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #132
The code block shown below contains an error. The code block intended to create a single- column DataFrame from Scala List years which is made up of integers. Identify the error. Code block…
The correct answer is B. The data type is not specified - the second argument to createDataset should be IntegerType. Option B is correct because createDataset requires an explicit Encoder (type specification) as a second argument - without it, Spark cannot determine how to serialize the data. The correct call would be spark.createDataset(years, Encoders.INT()), supplying the…
Question
The code block shown below contains an error. The code block intended to create a single- column DataFrame from Scala List years which is made up of integers. Identify the error. Code block:
spark.createDataset(years)
Options
- AThe years list should be wrapped in another list like List(years) to make clear that it is a column
- BThe data type is not specified - the second argument to createDataset should be IntegerType.
- CThere is no operation createDataset - the createDataFrame operation should be used instead.
- DThe result of the above is a Dataset rather than a DataFrame - the toDF operation must be called
- EThe column name must be specified as the second argument to createDataset.
How the community answered
(52 responses)- A15% (8)
- B73% (38)
- C2% (1)
- D4% (2)
- E6% (3)
Explanation
Option B is correct because createDataset requires an explicit Encoder (type specification) as a second argument - without it, Spark cannot determine how to serialize the data. The correct call would be spark.createDataset(years, Encoders.INT()), supplying the IntegerType-equivalent encoder.
Why the distractors are wrong:
- A - Wrapping in
List(years)would produce a Dataset of Lists, not a flat single-column structure. - C -
createDatasetis a valid Spark API method; no substitution is needed. - D - While
createDatasetdoes return aDataset[T](not aDataFrame), this is a type-conversion consideration, not the root error in the call itself. - E -
createDatasethas no column-name parameter; single-column Datasets default to a column named"value".
Memory tip: Think "Dataset needs Data-type" - whenever you call createDataset, you must supply the encoder that tells Spark how to handle the type, otherwise the call is incomplete.
Topics
Community Discussion
No community discussion yet for this question.