nerdexam
Databricks

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…

Spark DataFrame and Dataset Operations - API Usage

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)
  • A
    15% (8)
  • B
    73% (38)
  • C
    2% (1)
  • D
    4% (2)
  • E
    6% (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 - createDataset is a valid Spark API method; no substitution is needed.
  • D - While createDataset does return a Dataset[T] (not a DataFrame), this is a type-conversion consideration, not the root error in the call itself.
  • E - createDataset has 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

#Spark Datasets#API methods#Type specification#DataFrame creation

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice