-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add ParquetFileMerger for efficient row-group level file merging #14435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7f2d5b0
fa1d073
7a34353
c150887
c593e9e
c71b419
4ddb5b4
4130a79
4e4874e
eabaa0d
55aa295
047f9b6
0709582
cdc322d
853fd19
4c4f2cb
aa5fc36
5962e74
2eca995
45b0197
3194f1e
66532a3
417e0fa
2404008
a471220
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.iceberg.parquet; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.parquet.format.converter.ParquetMetadataConverter; | ||
| import org.apache.parquet.hadoop.ParquetFileReader; | ||
| import org.apache.parquet.hadoop.ParquetFileWriter; | ||
| import org.apache.parquet.hadoop.util.HadoopInputFile; | ||
| import org.apache.parquet.schema.MessageType; | ||
|
|
||
| /** | ||
| * Utility class for performing strict schema validation and merging of Parquet files at the | ||
| * row-group level. | ||
| * | ||
| * <p>This class ensures that all input files have identical Parquet schemas before merging. The | ||
| * merge operation is performed by copying row groups directly without | ||
| * serialization/deserialization, providing significant performance benefits over traditional | ||
| * read-rewrite approaches. | ||
| * | ||
| * <p>TODO: Encrypted tables are not supported | ||
|
pvary marked this conversation as resolved.
Outdated
|
||
| * | ||
| * <p>Key features: | ||
| * | ||
| * <ul> | ||
| * <li>Zero-copy row group merging using {@link ParquetFileWriter#appendFile} | ||
| * <li>Strict schema validation - all files must have identical {@link MessageType} | ||
| * <li>Metadata merging for Iceberg-specific footer data | ||
| * </ul> | ||
| * | ||
| * <p>Typical usage: | ||
| * | ||
| * <pre> | ||
| * Configuration conf = new Configuration(); | ||
| * List<Path> inputFiles = Arrays.asList(file1, file2, file3); | ||
| * Path outputFile = new Path("/path/to/output.parquet"); | ||
| * ParquetFileMerger.mergeFiles(inputFiles, outputFile, conf); | ||
| * </pre> | ||
| */ | ||
| public class ParquetFileMerger { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please describe me why we order the methods in this file, as we do it? In several comment I have mentioned that please make sure that we follow some logic here.
Or:
I would prefer the first, or migth be accept the one you have chosen, but I don't understand the logic as it stands.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump. @shangxinli: Could you please take a look at this comment? |
||
|
|
||
| private ParquetFileMerger() { | ||
| // Utility class - prevent instantiation | ||
| } | ||
|
|
||
| /** | ||
| * Merges multiple Parquet files into a single output file at the row-group level. | ||
| * | ||
| * <p>All input files must have identical Parquet schemas ({@link MessageType}), otherwise an | ||
| * exception is thrown. The merge is performed by copying row groups directly without | ||
| * serialization/deserialization. | ||
| * | ||
| * @param inputFiles List of input Parquet file paths to merge | ||
| * @param outputFile Output file path for the merged result | ||
| * @param conf Hadoop configuration to use for file operations | ||
| * @throws IOException if I/O error occurs during merge operation | ||
| * @throws IllegalArgumentException if no input files provided or schemas don't match | ||
| */ | ||
| public static void mergeFiles(List<Path> inputFiles, Path outputFile, Configuration conf) | ||
| throws IOException { | ||
| mergeFiles(inputFiles, outputFile, null, conf); | ||
| } | ||
|
Comment on lines
+146
to
+150
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should close these resources; otherwise they can cause file-handle leaks and lead to more serious problems. Please check other places as well.Thanks
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Guosmilesmile Good catch! You're absolutely right - the original code wasn't closing the ParquetFileReader properly. I've updated it to use try-with-resources: private static MessageType readSchema(InputFile inputFile) throws IOException { This uses Java 9's enhanced try-with-resources syntax to ensure the reader is properly closed. I've also verified that all other ParquetFileReader instances in the file (lines 166, 226, 268) are already using try-with-resources correctly. Thanks for catching this! |
||
|
|
||
| /** | ||
| * Merges multiple Parquet files into a single output file at the row-group level with custom | ||
| * metadata. | ||
| * | ||
| * <p>All input files must have identical Parquet schemas ({@link MessageType}), otherwise an | ||
| * exception is thrown. The merge is performed by copying row groups directly without | ||
| * serialization/deserialization. | ||
| * | ||
| * @param inputFiles List of input Parquet file paths to merge | ||
| * @param outputFile Output file path for the merged result | ||
| * @param extraMetadata Additional metadata to include in the output file footer (can be null) | ||
| * @param conf Hadoop configuration to use for file operations | ||
| * @throws IOException if I/O error occurs during merge operation | ||
| * @throws IllegalArgumentException if no input files provided or schemas don't match | ||
| */ | ||
|
Comment on lines
+152
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The javadoc is nice here, but remove the paramters.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
| public static void mergeFiles( | ||
|
pvary marked this conversation as resolved.
Outdated
|
||
| List<Path> inputFiles, Path outputFile, Map<String, String> extraMetadata, Configuration conf) | ||
| throws IOException { | ||
| // Validate and get the common schema | ||
| MessageType schema = validateAndGetSchema(inputFiles, conf); | ||
|
|
||
| // Create the output Parquet file writer | ||
| try (ParquetFileWriter writer = | ||
| new ParquetFileWriter(conf, schema, outputFile, ParquetFileWriter.Mode.CREATE)) { | ||
|
|
||
| writer.start(); | ||
|
|
||
| // Append each input file's row groups to the output | ||
| for (Path inputFile : inputFiles) { | ||
| writer.appendFile(HadoopInputFile.fromPath(inputFile, conf)); | ||
|
pvary marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // End writing with optional metadata | ||
| if (extraMetadata != null && !extraMetadata.isEmpty()) { | ||
| writer.end(extraMetadata); | ||
| } else { | ||
| writer.end(java.util.Collections.emptyMap()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validates that all input files have identical Parquet schemas and returns the common schema. | ||
| * | ||
| * <p>This method reads the Parquet metadata from each file and compares their schemas. If any | ||
| * schema differs, an {@link IllegalArgumentException} is thrown with details about the mismatch. | ||
| * | ||
| * @param inputFiles List of input Parquet file paths to validate | ||
| * @param conf Hadoop configuration to use for file operations | ||
| * @return The common {@link MessageType} schema shared by all input files | ||
| * @throws IOException if I/O error occurs while reading file metadata | ||
| * @throws IllegalArgumentException if no input files provided or schemas don't match | ||
| */ | ||
| private static MessageType validateAndGetSchema(List<Path> inputFiles, Configuration conf) | ||
| throws IOException { | ||
| Preconditions.checkArgument( | ||
| inputFiles != null && !inputFiles.isEmpty(), "No input files provided for merging"); | ||
|
|
||
| // Read the schema from the first file | ||
| MessageType firstSchema = | ||
| ParquetFileReader.readFooter(conf, inputFiles.get(0), ParquetMetadataConverter.NO_FILTER) | ||
| .getFileMetaData() | ||
| .getSchema(); | ||
|
|
||
| // Validate all remaining files have the same schema | ||
| for (int i = 1; i < inputFiles.size(); i++) { | ||
| MessageType currentSchema = | ||
| ParquetFileReader.readFooter(conf, inputFiles.get(i), ParquetMetadataConverter.NO_FILTER) | ||
| .getFileMetaData() | ||
| .getSchema(); | ||
|
|
||
| if (!firstSchema.equals(currentSchema)) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "Schema mismatch detected: file '%s' has schema %s but file '%s' has schema %s. " | ||
| + "All files must have identical Parquet schemas for row-group level merging.", | ||
| inputFiles.get(0), firstSchema, inputFiles.get(i), currentSchema)); | ||
| } | ||
| } | ||
|
|
||
| return firstSchema; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a list of Parquet files can be merged (i.e., they all have identical schemas). | ||
| * | ||
| * <p>This is a non-throwing version of {@link #validateAndGetSchema(List, Configuration)} that | ||
| * returns a boolean instead of throwing an exception. | ||
| * | ||
| * @param inputFiles List of input Parquet file paths to check | ||
| * @param conf Hadoop configuration to use for file operations | ||
| * @return true if all files have identical schemas and can be merged, false otherwise | ||
| */ | ||
| public static boolean canMerge(List<Path> inputFiles, Configuration conf) { | ||
| try { | ||
| validateAndGetSchema(inputFiles, conf); | ||
| return true; | ||
| } catch (IllegalArgumentException | IOException e) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.