Creating DataFrames from files (CSV, JSON, Parquet)
📖 Scenario: You work as a data analyst at a retail company. You receive sales data in different file formats: CSV, JSON, and Parquet. Your task is to load these files into Spark DataFrames to analyze the sales.
🎯 Goal: Learn how to create Spark DataFrames by reading data from CSV, JSON, and Parquet files.
📋 What You'll Learn
Use SparkSession to read files
Read a CSV file into a DataFrame
Read a JSON file into a DataFrame
Read a Parquet file into a DataFrame
Print the schema of each DataFrame
💡 Why This Matters
🌍 Real World
Data scientists and analysts often receive data in different file formats. Knowing how to load these files into Spark DataFrames is essential for data processing and analysis.
💼 Career
This skill is important for roles like Data Engineer, Data Scientist, and Big Data Analyst who work with large datasets stored in various formats.
Progress0 / 4 steps
1
Create a SparkSession
Create a SparkSession called spark with the app name "FileReaderApp".
Apache Spark
Hint
Use SparkSession.builder.appName(...).getOrCreate() to create the SparkSession.
2
Read the CSV file into a DataFrame
Use the spark session to read the CSV file named "sales.csv" into a DataFrame called csv_df. Assume the CSV file has a header row.
Apache Spark
Hint
Use spark.read.option("header", True).csv("sales.csv") to read the CSV file with headers.
3
Read the JSON and Parquet files into DataFrames
Use the spark session to read the JSON file named "sales.json" into a DataFrame called json_df, and the Parquet file named "sales.parquet" into a DataFrame called parquet_df.
Apache Spark
Hint
Use spark.read.json(...) and spark.read.parquet(...) to read JSON and Parquet files respectively.
4
Print the schema of each DataFrame
Print the schema of csv_df, json_df, and parquet_df using the .printSchema() method.
Apache Spark
Hint
Use printSchema() on each DataFrame to see its structure.
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?