Launch price: every exam for $29.99 a year, for the first 300 buyers. See the price

CertStreak
Menu

Implement schema enforcement and manage schema drift

A worked example

Single answer

A data engineer runs the Auto Loader stream shown in the exhibit. The stream does not set cloudFiles.schemaEvolutionMode. Its tracked schema and target table do not contain phone_number. A source system will soon start sending this new field.

Requirements:

  1. phone_number must be added to the schema that Auto Loader tracks in its schema location.
  2. No one may write or edit column definitions by hand.
  3. Ingestion must continue to use this incremental Auto Loader stream; restarting the stream is acceptable.

(spark.readStream .format("cloudFiles") .option("cloudFiles.format", "json") .option("cloudFiles.schemaLocation", "/Volumes/sales/bronze/landing/_schema") .load("/Volumes/sales/bronze/landing/customers/") .writeStream .option("mergeSchema", "true") .option("checkpointLocation", "/Volumes/sales/bronze/landing/_checkpoint") .toTable("sales.bronze.customers"))

Which approach should the engineer use to handle the upcoming field?

Choose one answer
Why

The reader uses the default addNewColumns mode because neither an explicit schema nor an evolution mode is supplied. Auto Loader updates its tracked schema when it detects the new field, then stops; restarting resumes processing with that schema. The writer's mergeSchema option allows the Delta target to accept the added column. Rescue does not evolve the tracked schema, failOnNewColumns requires manual intervention, and a batch replacement violates the streaming requirement.

  • Not correct: Configure cloudFiles.schemaEvolutionMode as failOnNewColumns before the new field arrives. This mode stops on a new field without automatically updating the schema. Meeting the requirement to add phone_number would require a manual schema update.
  • Not correct: Configure cloudFiles.schemaEvolutionMode as rescue before the new field arrives. Rescue mode preserves unexpected field values separately without evolving the tracked schema, so phone_number would not become a tracked column.
  • Correct: Keep the current configuration and restart the stream after it stops on the new field. Without an explicitly supplied schema or evolution mode, Auto Loader uses addNewColumns. It automatically updates its tracked schema before stopping, and restarting resumes processing. The mergeSchema write option permits the target table to accept the new column.
  • Not correct: Replace the stream with a batch append that sets mergeSchema to true. A mergeSchema batch append can add new columns to a table, but it replaces the Auto Loader stream, which the requirements do not allow.

Practise this topic until it's automatic.

Ten questions a day, every answer explained, and misses that come back until you beat them.

Start free