Parquet
Apache Parquet
A column-oriented file format. The big data analytics standard.
Apache Parquet
A column-oriented file format. The big data analytics standard.
Apache Parquet is a columnar file format jointly developed by Cloudera and Twitter in 2013. It is the de facto standard in big data analytics, and it is natively supported by Spark, Pandas, Polars, DuckDB, and major cloud services (AWS Athena, Google BigQuery, Snowflake).
Column-based storage is optimal for analytical queries. In SELECT name, salary FROM employees, the age and department columns are not read, which reduces I/O. Because data of the same type is stored contiguously, the compression efficiency of dictionary encoding, RLE (Run-Length Encoding), and delta encoding is maximized.
Data is split into units of Row Groups (128MB by default), and min/max statistics for each column are stored in each Row Group. For a WHERE age > 30 query, the statistics are checked and Row Groups that do not match the condition are skipped (Predicate Pushdown). Because the schema is embedded in the file, separate schema management is unnecessary.
In Python, files are read and written with the PyArrow or fastparquet library. Saving a Pandas DataFrame as Parquet makes it 5-10 times smaller than CSV, and reading is also faster. When building a data lake, using Parquet instead of JSON or CSV is recommended.
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
# Pandas DataFrame ์์ฑ
df = pd.DataFrame({
'user_id': range(1000000),
'name': ['user_' + str(i) for i in range(1000000)],
'age': [20 + i % 50 for i in range(1000000)],
'signup_date': pd.date_range('2020-01-01', periods=1000000, freq='T'),
'is_active': [i % 2 == 0 for i in range(1000000)]
})
# Parquet ํ์ผ๋ก ์ ์ฅ (๊ธฐ๋ณธ snappy ์์ถ)
df.to_parquet('users.parquet', index=False)
# ์์ถ ๋ฐฉ์ ์ง์ (gzip, snappy, zstd, lz4)
df.to_parquet('users_gzip.parquet', compression='gzip')
# ํน์ ์ปฌ๋ผ๋ง ์ฝ๊ธฐ (I/O ์ต์ํ)
df_subset = pd.read_parquet('users.parquet', columns=['user_id', 'name'])
# ํํฐ ์กฐ๊ฑด ์ ์ฉ (Predicate Pushdown)
df_filtered = pd.read_parquet(
'users.parquet',
filters=[('age', '>=', 30), ('is_active', '==', True)]
)
# PyArrow๋ก ์ง์ ์ ์ด
table = pa.Table.from_pandas(df)
# ํํฐ์
์ ์ฅ (Hive ์คํ์ผ)
pq.write_to_dataset(
table,
root_path='users_partitioned',
partition_cols=['is_active']
)
# ๊ฒฐ๊ณผ: users_partitioned/is_active=true/part-0.parquet
# users_partitioned/is_active=false/part-0.parquet
# ํํฐ์
๋ ๋ฐ์ดํฐ์
์ฝ๊ธฐ
dataset = pq.ParquetDataset('users_partitioned')
df_partitioned = dataset.read().to_pandas()
# ๋ฉํ๋ฐ์ดํฐ ํ์ธ
parquet_file = pq.ParquetFile('users.parquet')
print(f"Row Groups: {parquet_file.metadata.num_row_groups}")
print(f"Columns: {parquet_file.schema.names}")
print(f"Total Rows: {parquet_file.metadata.num_rows}")
# ์ปฌ๋ผ๋ณ ํต๊ณ ํ์ธ
for i in range(parquet_file.metadata.num_row_groups):
rg = parquet_file.metadata.row_group(i)
for j in range(rg.num_columns):
col = rg.column(j)
if col.statistics:
print(f"Column {col.path_in_schema}: "
f"min={col.statistics.min}, max={col.statistics.max}")
# Spark์์ Parquet ์ฌ์ฉ
# spark.read.parquet("users.parquet")
# df.write.partitionBy("is_active").parquet("output_path")
Senior: "After we switched what we'd been piling up in the data lake as CSV over to Parquet, our Athena query costs dropped by 80%. It's thanks to column pruning."
Junior: "What criteria do you use for partitioning?"
Senior: "Use the columns you frequently filter on in queries. If you partition by date, a WHERE date='2024-01-01' query scans only that partition."
Interviewer: "What are your criteria for choosing a file format in a data pipeline?"
Candidate: "I use Parquet for analytical workloads and Avro for log streaming. With column pruning and Predicate Pushdown, Parquet makes analytical queries fast, and Avro is well suited to streaming because schema evolution is easy. For data lakes, I standardize on Parquet so that Spark, Athena, and Pandas can all access the data consistently."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "The Row Group size is too small. If there are hundreds of Row Groups per file, the metadata overhead gets large."
Developer: "Can I just increase row_group_size in the write options?"
Reviewer: "Yes, or merge the small files and save them. Around 128MB is appropriate."