Data Engineering · Chapter 3 · ~15 min · Intermediate
File Formats Deep Dive: CSV, JSON & Apache Parquet
Compare row-oriented vs columnar storage, dictionary encoding, run-length compression, and predicate pushdown in Parquet files.
- Contrast row-oriented formats (CSV, JSON) with columnar binary storage (Apache Parquet)
- Understand dictionary encoding and run-length compression algorithms for 80%+ storage savings
- Master predicate pushdown and column projection to eliminate unnecessary disk I/O
- Inspect Parquet metadata structures: FileMetaData, RowGroups, and Min/Max column statistics
File Formats Deep Dive: CSV, JSON & Apache Parquet: learn it visually
Compare row-oriented vs columnar storage, dictionary encoding, run-length compression, and predicate pushdown in Parquet files.
1The Plain-Text Trap: Why Big Data Cannot Run on CSV or JSON
In early programming, text-based formats like CSV (Comma-Separated Values) and JSON (JavaScript Object Notation) feel effortless. They are human-readable, editable in any text editor, and supported by every programming language on earth.
However, at enterprise scale (millions or billions of rows), text formats create catastrophic performance bottlenecks:
Row-Oriented Layout
To read 1 column out of 50, the storage engine must scan 100% of the bytes across all 50 columns on disk.
Expensive Text Parsing
CPUs must parse ASCII strings byte by byte, check escape quotes, and parse strings into binary numbers like floats or timestamps.
Lack of Metadata
Neither CSV nor JSON stores column data types or min/max statistics. The engine must scan every single line to evaluate filters.
Why does calculating the average of a single column in a 50-column CSV file cause high disk I/O?
2Under the Hood: Apache Parquet Anatomy
Apache Parquet is an open-source, columnar, binary storage format created specifically for analytics. Parquet files are structured into three distinct layers:
1. Row Groups
A logical horizontal partitioning of the dataset into chunks of 100,000 to 1,000,000 rows (typically 128 MB to 512 MB each).
2. Column Chunks
Within each Row Group, data for each specific column is stored contiguously in Pages. If a query only requests revenue, only the revenue Column Chunk is fetched.
3. File Metadata Footer
Stored at the end of the Parquet file! The footer contains the exact byte offsets of every column chunk, schemas, and summary statistics (min value, max value, null count) for every column in every Row Group.
Wrong idea: CSV / JSON reads only requested columns
Assuming that running SELECT SUM(salary) FROM employees.csv only loads the salary column numbers into memory.
Real system: Parquet Columnar Projection: Read strictly needed columns
Parquet groups values by column contiguously. Reading SUM(salary) loads only the binary salary chunk, bypassing all other columns.
How does 'Predicate Pushdown' enable Parquet query engines to avoid reading gigabytes of data from disk?
3Compression Algorithms: Dictionary, RLE & Bit Packing
Because Parquet groups values of the same data type together, compression algorithms achieve staggering ratios (often 80% to 95% reduction compared to raw text):
# Example: Dictionary Encoding & Bit Packing
# Raw repetitive column data:
# ["ACTIVE", "ACTIVE", "INACTIVE", "ACTIVE", "PENDING", "ACTIVE"]
# Parquet creates a compact Dictionary Page:
dictionary = {
0: "ACTIVE",
1: "INACTIVE",
2: "PENDING"
}
# The Data Page stores only 2-bit integer indexes:
# [0, 0, 1, 0, 2, 0] -> Can be packed into a single byte!
# Followed by Snappy or Zstandard (ZSTD) block compression.Why does Dictionary Encoding work exceptionally well on low-cardinality columns (e.g. state code, order status, country)?
4Hands-On: Test Column Projection & Predicate Pushdown
Experiment with different analytical queries below. Observe how switching from CSV to Apache Parquet dramatically drops disk bytes read and query execution time:
Interactive Columnar Engine & Predicate Pushdown Simulator
sales (1,000,000 rows, 6 columns)Parquet divides 1M rows into 4 Row Groups. Each Row Group stores columns separately with min/max footer metadata.
5Bug Hunt: Optimize a High-Volume Parquet Query
An analyst wrote a DuckDB Python script that queries daily sales Parquet files. However, it crashes production memory by reading every column and running an unvectorized Python loop. Fix all three query optimization bugs below and press Run & verify:
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Replace
SELECT *with targeted column projection or aggregation (e.g.SELECT SUM(revenue)). - Push the
WHERE country = 'US'filter into the SQL query so DuckDB prunes Parquet row groups at read time. - Eliminate the Python loop by computing
SUM(revenue)in SQL directly.
6Recap
- CSV and JSON are row-oriented text formats requiring complete file scans and CPU-intensive ASCII parsing for every analytical query.
- Apache Parquet is a binary, columnar storage standard that groups data into Row Groups, Column Chunks, and Pages.
- Column Projection allows analytical engines to read only the specific columns requested in a
SELECTstatement. - Predicate Pushdown uses Row Group min/max statistics stored in the Parquet footer to skip entire 128 MB blocks without reading data pages.
- Dictionary Encoding & Bit Packing achieve up to 90%+ storage reduction on repetitive categorical data.
Ready for Chapter 4? Mark this chapter as complete to proceed: