A DataFrame is a table-like data structure in Spark that holds data in rows and columns, similar to a spreadsheet or SQL table. It allows easy data manipulation and analysis.
Click to reveal answer
beginner
How do you read a CSV file into a Spark DataFrame?
Use spark.read.csv('file_path', header=True, inferSchema=True) to load a CSV file. 'header=True' means the first row is column names, and 'inferSchema=True' lets Spark guess data types.
Click to reveal answer
intermediate
What is the difference between reading JSON and Parquet files in Spark?
JSON files are text-based and semi-structured, read with spark.read.json('file_path'). Parquet files are binary, columnar, and optimized for speed, read with spark.read.parquet('file_path').
Click to reveal answer
intermediate
Why use Parquet files instead of CSV or JSON in Spark?
Parquet files are faster to read and use less storage because they store data in a compressed, column-based format. This makes them better for big data processing.
Click to reveal answer
beginner
What does the 'inferSchema' option do when reading CSV files in Spark?
'inferSchema=True' tells Spark to automatically detect the data type of each column instead of treating all columns as strings.
Click to reveal answer
Which Spark method reads a JSON file into a DataFrame?
Aspark.read.parquet('file_path')
Bspark.read.json('file_path')
Cspark.read.csv('file_path')
Dspark.read.text('file_path')
✗ Incorrect
Use spark.read.json() to load JSON files into a DataFrame.
What option should you set to True to use the first row as column names when reading a CSV?
AinferSchema=True
Bdelimiter=','
Cheader=True
Dschema=True
✗ Incorrect
Setting header=True tells Spark the first row contains column names.
Which file format is columnar and optimized for fast reading in Spark?
AParquet
BJSON
CCSV
DTXT
✗ Incorrect
Parquet is a columnar storage format designed for fast data processing.
What does 'inferSchema=True' do when reading CSV files?
AAutomatically detects column data types
BUses the first row as data
CSkips empty rows
DReads the file faster
✗ Incorrect
It lets Spark guess the correct data types for each column.
Which method reads Parquet files in Spark?
Aspark.read.text()
Bspark.read.json()
Cspark.read.csv()
Dspark.read.parquet()
✗ Incorrect
spark.read.parquet() loads Parquet files into DataFrames.
Explain how to create a Spark DataFrame from a CSV file including important options.
Think about telling Spark where the file is and how to treat the first row and data types.
You got /4 concepts.
Describe the advantages of using Parquet files over CSV or JSON in Spark.
Focus on speed and storage benefits.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a DataFrame from a file in Apache Spark?
easy
A. To write data back to a file
B. To organize data in rows and columns for easy analysis
C. To delete data from the file system
D. To convert data into plain text format
Solution
Step 1: Understand DataFrame concept
A DataFrame is a table-like structure with rows and columns used for data analysis.
Step 2: Purpose of loading files
Loading files into DataFrames allows Spark to process and analyze data efficiently.
Final Answer:
To organize data in rows and columns for easy analysis -> Option B
Quick Check:
DataFrames = organized data [OK]
Hint: DataFrames = tables for easy data work [OK]
Common Mistakes:
Confusing DataFrame creation with file writing
Thinking DataFrames delete files
Assuming DataFrames convert data to plain text
2. Which of the following is the correct syntax to load a CSV file into a Spark DataFrame?
easy
A. spark.read.format('csv').load('file.csv')
B. spark.read.load('file.csv', format='csv')
C. spark.load('csv', 'file.csv')
D. spark.read.csv('file.csv')
Solution
Step 1: Recall Spark CSV loading syntax
The standard way is to use spark.read.format('csv').load('filename').
Step 2: Check each option
spark.read.format('csv').load('file.csv') matches the correct syntax; others are incorrect or invalid methods.
Final Answer:
spark.read.format('csv').load('file.csv') -> Option A
Quick Check:
Use format('csv').load() for CSV [OK]
Hint: Use spark.read.format('csv').load() for CSV files [OK]
Common Mistakes:
Using spark.read.csv() without options
Trying spark.load() which doesn't exist
Passing format as a keyword argument in load()
3. Given the following code, what will be the schema of the DataFrame?