Nobody sets out to build a lake of ten thousand tiny files. It happens by itself: a job appends every five minutes, a backfill writes one file per day, and a year later every query spends longer opening files than reading them. The data did not change, the layout decayed. Knowing how to see it and fix it is routine lake maintenance.
The idea in plain words
Where we are. Everything so far, row groups, statistics, partitions, assumed files of sensible size. This topic is about what goes wrong over time and how to repair it with the metadata alone. The capstone puts the whole layout together, including how often to compact.
Rewriting many small files as a few large, well-organised ones without changing the data. It lowers the per-file costs of listing, opening and reading footers, and gives compression and statistics enough rows to work with.
Make the mess, then measure it
write_small_files cuts the table into slices of 200 rows, one file each, as an appending job would. file_rows then lists the files and their row counts from the footers alone, which is how you would audit a real lake without paying to read it. The totals show the cost: the same rows take more bytes in many files than in one.
# Write the bars as files of 200 rows, then audit them from the footersos.makedirs("parts", exist_ok=True)print(write_small_files(table, "parts", 200), "files written")files = file_rows("parts")print(files[:2], "...", files[-1])print("bytes in all the small files:", sum(os.path.getsize("parts/" + name) for name, _ in files))
168 files written
[('part-0000.parquet', 200), ('part-0001.parquet', 200)] ... ('part-0167.parquet', 93)
bytes in all the small files: 1762336# The candidates for compaction: files below 500 rowssmall = small_files("parts", 500)print(len(small), "of", len(files), "files are below 500 rows")
168 of 168 files are below 500 rows
Compact: many files in, one file out
compact reads the files in name order, concatenates them and writes one file with row groups of a size you choose. Because the small files were written in order, the result is the original table, now in a handful of row groups whose statistics are tight and whose pages compress well. The small files are left in place: delete them only after the new file has been checked.
# Merge all the small files into one file with row groups of 10,000 rows, and comparemerged = compact("parts", "compacted.parquet", 10000)print("files merged, row groups written:", merged)big = os.path.getsize("compacted.parquet")print("bytes in the one file:", big)print("same rows:", pq.read_table("compacted.parquet").equals(table))
files merged, row groups written: (168, 4) bytes in the one file: 1268429 same rows: True
Compact the partitions that stopped receiving writes: yesterday's folder, not today's. Aim for files of 128 MB to 1 GB, check that the row counts match before deleting anything, and keep readers working throughout, which usually means writing the new file under a new name and then swapping. Table formats such as Iceberg and Delta automate exactly this.
What the tasks ask
- 1`write_small_files`Loop
range(0, table.num_rows, rows_per_file), writetable.slice(start, rows_per_file)aspart-NNNN.parquet, count the files. - 2`file_rows` and `small_files`
pq.ParquetFile(path).metadata.num_rowsfor each.parquetfile in sorted order; then keep the names belowmin_rows. - 3`compact`
pa.concat_tablesof the files read in name order;pq.write_table(..., row_group_size=...); return the file count and the new file's row-group count; with no files, return(0, 0).
Lab · what's graded
4 tasks · 100 pts- Make the mess:
write_small_files25 pts - Count rows from the footers:
file_rows25 pts - Find the small ones:
small_files25 pts - Merge them:
compact25 pts