Compact a Parquet dataset into fewer, larger files
Source:R/pro_compact_parquet.R
pro_compact_parquet.Rdpro_request_parquet() writes one Parquet file per API page (up to 200
records). That keeps the conversion parallel and resumable, but a large
query becomes thousands of small files, which are slow to read.
pro_compact_parquet() combines them into files of up to rows records.
Records, columns and their order do not change.
Arguments
- parquet_dir
Output directory of
pro_request_parquet(), or theparquetfolder of apro_fetch()project.- rows
Maximum number of records per compacted file, e.g.
250000.- verbose
Logical. Report what was compacted. Default
TRUE.
Details
Measured for 1.1 million works (5,460 page files): compaction took about
20 seconds; afterwards aggregates and lookups by ID ran 35 to 50 times
faster, reading full records with DuckDB 4 times faster, and the dataset
was 40% smaller. Files of 50,000 to 1 million records made no difference
to read speed; rows = 250000 is a good choice (about 0.25 GB of full
works records; newer works with abstracts and long reference lists are
larger).
Each query's partition (
query=<name>/) is compacted on its own; its_query.jsonand_query_oql.*files stay.A compacted file holds whole consecutive pages, in download order, and is named after them: pages 1 to 1250 become
results_pages_1-1250.parquet. A page with more thanrowsrecords becomes a file of its own. Thepagecolumn is kept.Files are compacted one at a time: each is written to a
.partfile, its record count checked, renamed, and only then are its pages deleted. If interrupted, run it again: it completes what was started.Files compacted before are left as they are; only page files are combined.
If a partition cannot be compacted (for example because its pages have conflicting column types), a warning says so and its page files stay: the dataset is complete either way.
Indexes built by
openalexSnapshot::build_corpus_index()orbuild_doi_index()point to files and row numbers, and would be wrong after compaction. If such an index exists next toparquet_dir, the function stops: delete the index, compact, and build it again.
See also
The compact_parquet argument of pro_request_parquet() and
pro_fetch(), which compacts right after the conversion.