But you get an error: TypeError: StructField() missing 1 required positional argument: 'nullable'. What is the fix?
medium
A. Add the 'nullable' argument to each StructField, e.g. StructField('id', IntegerType(), True)
B. Remove the schema and let Spark infer it
C. Change StructType to SchemaType
D. Use a list of tuples instead of StructField
Solution
Step 1: Understand StructField parameters
StructField requires three arguments: name, data type, and nullable (True/False).
Step 2: Fix the code by adding nullable
Add the third argument 'nullable=True' to each StructField to fix the error.
Final Answer:
Add the 'nullable' argument to each StructField, e.g. StructField('id', IntegerType(), True) -> Option A
Quick Check:
StructField needs nullable argument [OK]
Hint: Always include nullable in StructField [OK]
Common Mistakes:
Forgetting nullable argument
Trying to rename StructType
Ignoring error and running code
5. You have a CSV file with columns: 'user', 'score', and 'date'. You want to read it with Spark and ensure 'score' is an integer and 'date' is a date type. Which schema definition and read method is correct to validate this?