What is the purpose of using options when reading CSV files in Apache Spark?
Options help customize how Spark reads CSV files, such as setting the delimiter, header presence, and handling null values. This makes data reading flexible and accurate.
Click to reveal answer
beginner
How do you tell Spark that the CSV file has a header row?
You use the option "header" set to "true" like this: .option("header", "true"). This tells Spark to treat the first row as column names.
Click to reveal answer
intermediate
What does the option "inferSchema" do when reading a CSV file?
Setting .option("inferSchema", "true") tells Spark to automatically detect the data types of each column instead of treating all data as strings.
Click to reveal answer
beginner
How can you change the delimiter used in a CSV file when reading it with Spark?
Use the option "sep" with the delimiter character, for example: .option("sep", ";") to use semicolon as the separator instead of comma.
Click to reveal answer
beginner
What happens if you do not specify the "header" option when reading a CSV file in Spark?
Spark treats all rows as data, and column names will be default names like _c0, _c1, etc. The first row will not be used as headers.
Click to reveal answer
Which option tells Spark to treat the first row of a CSV file as column headers?
Aheader = true
BinferSchema = true
Csep = ","
DnullValue = "null"
✗ Incorrect
The option header = true tells Spark to use the first row as column names.
What does setting inferSchema = true do when reading a CSV file?
AIt skips the first row
BIt changes the delimiter
CIt automatically detects column data types
DIt removes null values
✗ Incorrect
inferSchema = true makes Spark detect the data types of columns automatically.
How do you specify a semicolon as the delimiter in a CSV file read by Spark?
Aoption("delimiter", ";")
Boption("sep", ";")
Coption("header", ";")
Doption("inferSchema", ";")
✗ Incorrect
The option sep sets the delimiter character, so sep = ";" uses semicolon.
If you do not set header = true, what will Spark do with the first row?
ATreat it as data
BSkip it
CUse it as column names
DThrow an error
✗ Incorrect
Without header = true, Spark treats the first row as normal data.
Which option can you use to specify a string to treat as null in CSV data?
Asep
Bheader
CinferSchema
DnullValue
✗ Incorrect
nullValue option lets you specify which string should be treated as null.
Explain how to read a CSV file in Spark that has a header row, uses semicolon as delimiter, and requires automatic schema detection.
Think about the options needed for header, delimiter, and schema.
You got /4 concepts.
What are the effects of not setting the header option when reading CSV files in Spark?
Consider how Spark names columns and treats rows by default.
You got /3 concepts.
Practice
(1/5)
1. What does the header option do when reading a CSV file in Apache Spark?
easy
A. It tells Spark to treat the first line as column names.
B. It specifies the character used to separate columns.
C. It infers the data types of each column automatically.
D. It sets the file reading mode to permissive.
Solution
Step 1: Understand the header option
The header option tells Spark whether the first line of the CSV file contains column names or data.
Step 2: Identify its effect
If header is set to true, Spark uses the first line as column names; otherwise, it treats all lines as data.
Final Answer:
It tells Spark to treat the first line as column names. -> Option A
Quick Check:
header = first line as columns [OK]
Hint: Remember: header=true means first line is column names [OK]
Common Mistakes:
Confusing header with separator option
Thinking header infers data types
Assuming header controls error handling
2. Which of the following is the correct way to read a CSV file with a semicolon separator and header in Spark?
easy
A. spark.read.csv(options={'sep': ';', 'header': 'true'}, 'file.csv')
B. spark.read.csv(sep=';', 'file.csv', header=True)
C. spark.read.option('sep', ';').option('header', 'true').csv('file.csv')
D. spark.read.csv(header=True, sep=';', 'file.csv')
Solution
Step 1: Recall Spark CSV reading syntax
Spark uses option() method to set options before calling csv().
Step 2: Check each option usage
spark.read.option('sep', ';').option('header', 'true').csv('file.csv') correctly chains option('sep', ';') and option('header', 'true') before reading the file.
Final Answer:
spark.read.option('sep', ';').option('header', 'true').csv('file.csv') -> Option C
Quick Check:
Use option() chaining for CSV options [OK]
Hint: Use .option() chaining to set CSV read options [OK]
Common Mistakes:
Passing options directly inside csv() as kwargs
Using Python booleans instead of strings for options
Incorrect method chaining order
3. Given the code below, what will be the schema of the DataFrame?
A. The header option must be set to 'false' to read headers.
B. The separator should be a semicolon, not a comma.
C. The file path must be an absolute path.
D. The options should be set using the option() method, not as parameters in csv().
Solution
Step 1: Check how options are passed in Spark CSV reading
Spark requires options like header and sep to be set using option() method before calling csv().
Step 2: Identify the error in the code
The code incorrectly passes options as parameters inside csv(), which is not supported and causes an error.
Final Answer:
The options should be set using the option() method, not as parameters in csv(). -> Option D
Quick Check:
Use .option() for CSV options, not csv() params [OK]
Hint: Set CSV options with .option(), not inside csv() call [OK]
Common Mistakes:
Passing options directly inside csv()
Using Python booleans instead of strings for options
Confusing separator character
5. You want to read a CSV file that may have some corrupted rows. Which option should you use to skip corrupted rows and still read the rest of the data?
hard
A. mode='dropmalformed'
B. mode='failfast'
C. mode='permissive'
D. mode='ignore'
Solution
Step 1: Understand CSV reading modes in Spark
Spark supports modes like 'permissive', 'failfast', and 'dropmalformed' to handle corrupted rows.
Step 2: Identify mode to skip corrupted rows
The 'dropmalformed' mode skips corrupted rows and reads the rest without error.
Final Answer:
mode='dropmalformed' -> Option A
Quick Check:
Use dropmalformed to skip bad rows [OK]
Hint: Use mode='dropmalformed' to skip bad rows safely [OK]
Common Mistakes:
Using failfast which stops on error
Assuming permissive skips corrupted rows
Using ignore which is not a valid mode
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.