Complete the code to read a JSON file with schema enforcement in Spark.
df = spark.read.schema([1]).json("data.json")
The schema method requires a defined schema object like userSchema to enforce the schema when reading data.
Complete the code to enable schema evolution when writing a Delta table.
df.write.format("delta").option("[1]", "true").mode("append").save("/delta/events")
The option mergeSchema set to true allows schema evolution when appending data to a Delta table.
Fix the error in the code to enforce schema when reading a Parquet file.
df = spark.read.[1](userSchema).parquet("data.parquet")
The correct method to enforce a schema is schema(), not inferSchema or others.
Fill both blanks to create a DataFrame with a new column and enforce schema on write.
df2 = df.withColumn("new_col", [1]) df2.write.format("delta").option("[2]", "true").mode("append").save("/delta/events")
Use col("existing_col") + 1 to create a new column based on an existing one. Use mergeSchema option to allow schema evolution on write.
Fill all three blanks to create a schema, read JSON with enforcement, and show the DataFrame.
from pyspark.sql.types import StructType, StructField, [1], StringType userSchema = StructType([ StructField("name", StringType(), True), StructField("age", [2], True) ]) df = spark.read.schema(userSchema).json("users.json") df.[3]()
Use IntegerType to define the age field type. Use show() to display the DataFrame content.
