Challenge - 5 Problems
Schema Evolution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of schema enforcement with missing column
What will be the output when trying to read a DataFrame with a schema that requires a non-nullable column, but the data is missing that column?
Apache Spark
from pyspark.sql import SparkSession from pyspark.sql.types import StructType, StructField, StringType, IntegerType spark = SparkSession.builder.master('local').appName('SchemaTest').getOrCreate() schema = StructType([ StructField('name', StringType(), nullable=False), StructField('age', IntegerType(), nullable=False) ]) data = [('Alice',), ('Bob',)] rdd = spark.sparkContext.parallelize(data) df = spark.createDataFrame(rdd, ['name']) try: df_with_schema = spark.createDataFrame(df.rdd, schema) df_with_schema.show() except Exception as e: print(e)
Attempts:
2 left
💡 Hint
Spark pads missing columns with nulls, even if marked non-nullable.
✗ Incorrect
When a schema has more columns than the data, Spark pads the missing columns with nulls. The non-nullable flag is an optimization hint and does not prevent nulls during DataFrame creation.
❓ data_output
intermediateResult of schema evolution by adding a new nullable column
Given a DataFrame with schema having columns 'id' and 'name', what will be the result of reading new data that adds a nullable column 'email' without changing the original schema?
Apache Spark
from pyspark.sql import SparkSession from pyspark.sql.types import StructType, StructField, StringType, IntegerType spark = SparkSession.builder.master('local').appName('SchemaEvolution').getOrCreate() original_schema = StructType([ StructField('id', IntegerType(), nullable=False), StructField('name', StringType(), nullable=True) ]) # Original data data1 = [(1, 'Alice'), (2, 'Bob')] df1 = spark.createDataFrame(data1, schema=original_schema) df1.show() # New data with extra nullable column 'email' data2 = [(3, 'Charlie', 'charlie@example.com'), (4, 'David', None)] df2 = spark.createDataFrame(data2, ['id', 'name', 'email']) # Read new data with original schema try: df2_with_original_schema = spark.createDataFrame(df2.rdd, original_schema) df2_with_original_schema.show() except Exception as e: print(e)
Attempts:
2 left
💡 Hint
Think about how Spark handles extra columns when applying a schema with fewer columns.
✗ Incorrect
When applying a schema with fewer columns than the data, Spark reads only the columns defined in the schema and ignores extra columns without error.
🔧 Debug
advancedIdentify the cause of schema mismatch error
Why does the following code raise a schema mismatch error when reading JSON data with a schema expecting an integer, but the data has a string?
Apache Spark
from pyspark.sql import SparkSession from pyspark.sql.types import StructType, StructField, IntegerType spark = SparkSession.builder.master('local').appName('SchemaMismatch').getOrCreate() schema = StructType([ StructField('id', IntegerType(), nullable=False) ]) json_data = '{"id": "abc"}' df = spark.read.option("mode", "FAILFAST").schema(schema).json(spark.sparkContext.parallelize([json_data])) df.show()
Attempts:
2 left
💡 Hint
Check the data type of the value in JSON and the expected schema type.
✗ Incorrect
Spark tries to cast the JSON string 'abc' to integer but fails, causing a schema mismatch error.
❓ visualization
advancedVisualizing schema evolution with added and removed columns
You have two DataFrames: one with columns ['id', 'name', 'age'] and another with ['id', 'name', 'email']. You want to visualize the schema differences. Which visualization best shows added and removed columns clearly?
Attempts:
2 left
💡 Hint
Think about how to show common and different columns between two sets.
✗ Incorrect
A Venn diagram clearly shows which columns are shared and which are unique to each schema, making it ideal for visualizing schema evolution.
🚀 Application
expertHandling schema evolution in streaming data ingestion
You have a streaming pipeline ingesting JSON data with evolving schema: new nullable columns are added over time. How should you configure Spark Structured Streaming to handle schema evolution without failing on new columns?
Attempts:
2 left
💡 Hint
Consider how to allow new columns to appear without breaking the stream.
✗ Incorrect
Using a schema that includes all expected columns as nullable and enabling schema inference allows Spark to handle new columns gracefully during streaming ingestion.
