-
Notifications
You must be signed in to change notification settings - Fork 3k
[iceberg-1746] Implement spark fanout writer #1774
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e3e886
[iceberg-1746] Implement spark fanout writer
6f10277
[iceberg-1746] Implement spark fanout writer
600d230
[iceberg-1746] Implement spark fanout writer
0917c7c
[iceberg-1746] Implement spark fanout writer
03c1c77
[iceberg-1746] Implement spark fanout writer
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
spark/src/main/java/org/apache/iceberg/spark/source/SparkPartitionedFanoutWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * 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.spark.source; | ||
|
|
||
| import org.apache.iceberg.FileFormat; | ||
| import org.apache.iceberg.PartitionKey; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.io.FileAppenderFactory; | ||
| import org.apache.iceberg.io.FileIO; | ||
| import org.apache.iceberg.io.OutputFileFactory; | ||
| import org.apache.iceberg.io.PartitionedFanoutWriter; | ||
| import org.apache.spark.sql.catalyst.InternalRow; | ||
| import org.apache.spark.sql.types.StructType; | ||
|
|
||
| public class SparkPartitionedFanoutWriter extends PartitionedFanoutWriter<InternalRow> { | ||
| private final PartitionKey partitionKey; | ||
| private final InternalRowWrapper internalRowWrapper; | ||
|
|
||
| public SparkPartitionedFanoutWriter(PartitionSpec spec, FileFormat format, | ||
| FileAppenderFactory<InternalRow> appenderFactory, | ||
| OutputFileFactory fileFactory, FileIO io, long targetFileSize, | ||
| Schema schema, StructType sparkSchema) { | ||
| super(spec, format, appenderFactory, fileFactory, io, targetFileSize); | ||
| this.partitionKey = new PartitionKey(spec, schema); | ||
| this.internalRowWrapper = new InternalRowWrapper(sparkSchema); | ||
| } | ||
|
|
||
| @Override | ||
| protected PartitionKey partition(InternalRow row) { | ||
| partitionKey.partition(internalRowWrapper.wrap(row)); | ||
| return partitionKey; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,8 @@ | |
| import static org.apache.iceberg.TableProperties.COMMIT_TOTAL_RETRY_TIME_MS_DEFAULT; | ||
| import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT; | ||
| import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT_DEFAULT; | ||
| import static org.apache.iceberg.TableProperties.WRITE_PARTITIONED_FANOUT_ENABLED; | ||
| import static org.apache.iceberg.TableProperties.WRITE_PARTITIONED_FANOUT_ENABLED_DEFAULT; | ||
| import static org.apache.iceberg.TableProperties.WRITE_TARGET_FILE_SIZE_BYTES; | ||
| import static org.apache.iceberg.TableProperties.WRITE_TARGET_FILE_SIZE_BYTES_DEFAULT; | ||
|
|
||
|
|
@@ -83,6 +85,7 @@ class Writer implements DataSourceWriter { | |
| private final Schema writeSchema; | ||
| private final StructType dsSchema; | ||
| private final Map<String, String> extraSnapshotMetadata; | ||
| private final boolean partitionedFanoutEnabled; | ||
|
|
||
| Writer(Table table, Broadcast<FileIO> io, Broadcast<EncryptionManager> encryptionManager, | ||
| DataSourceOptions options, boolean replacePartitions, String applicationId, Schema writeSchema, | ||
|
|
@@ -113,6 +116,10 @@ class Writer implements DataSourceWriter { | |
| long tableTargetFileSize = PropertyUtil.propertyAsLong( | ||
| table.properties(), WRITE_TARGET_FILE_SIZE_BYTES, WRITE_TARGET_FILE_SIZE_BYTES_DEFAULT); | ||
| this.targetFileSize = options.getLong("target-file-size-bytes", tableTargetFileSize); | ||
|
|
||
| boolean tablePartitionedFanoutEnabled = PropertyUtil.propertyAsBoolean( | ||
|
Member
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. Q: will we set this for a given table ? In my option, it's per job ?
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. we need set this option for a given table, Because some tables require fanout. |
||
| table.properties(), WRITE_PARTITIONED_FANOUT_ENABLED, WRITE_PARTITIONED_FANOUT_ENABLED_DEFAULT); | ||
| this.partitionedFanoutEnabled = options.getBoolean("partitioned.fanout.enabled", tablePartitionedFanoutEnabled); | ||
| } | ||
|
|
||
| private FileFormat getFileFormat(Map<String, String> tableProperties, DataSourceOptions options) { | ||
|
|
@@ -131,7 +138,7 @@ private boolean isWapTable() { | |
| public DataWriterFactory<InternalRow> createWriterFactory() { | ||
| return new WriterFactory( | ||
| table.spec(), format, table.locationProvider(), table.properties(), io, encryptionManager, targetFileSize, | ||
| writeSchema, dsSchema); | ||
| writeSchema, dsSchema, partitionedFanoutEnabled); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -246,11 +253,12 @@ static class WriterFactory implements DataWriterFactory<InternalRow> { | |
| private final long targetFileSize; | ||
| private final Schema writeSchema; | ||
| private final StructType dsSchema; | ||
| private final boolean partitionedFanoutEnabled; | ||
|
|
||
| WriterFactory(PartitionSpec spec, FileFormat format, LocationProvider locations, | ||
| Map<String, String> properties, Broadcast<FileIO> io, | ||
| Broadcast<EncryptionManager> encryptionManager, long targetFileSize, | ||
| Schema writeSchema, StructType dsSchema) { | ||
| Schema writeSchema, StructType dsSchema, boolean partitionedFanoutEnabled) { | ||
| this.spec = spec; | ||
| this.format = format; | ||
| this.locations = locations; | ||
|
|
@@ -260,6 +268,7 @@ static class WriterFactory implements DataWriterFactory<InternalRow> { | |
| this.targetFileSize = targetFileSize; | ||
| this.writeSchema = writeSchema; | ||
| this.dsSchema = dsSchema; | ||
| this.partitionedFanoutEnabled = partitionedFanoutEnabled; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -270,9 +279,12 @@ public DataWriter<InternalRow> createDataWriter(int partitionId, long taskId, lo | |
|
|
||
| if (spec.fields().isEmpty()) { | ||
| return new Unpartitioned24Writer(spec, format, appenderFactory, fileFactory, io.value(), targetFileSize); | ||
| } else if (partitionedFanoutEnabled) { | ||
| return new PartitionedFanout24Writer(spec, format, appenderFactory, fileFactory, io.value(), targetFileSize, | ||
| writeSchema, dsSchema); | ||
| } else { | ||
| return new Partitioned24Writer(spec, format, appenderFactory, fileFactory, io.value(), | ||
| targetFileSize, writeSchema, dsSchema); | ||
| return new Partitioned24Writer(spec, format, appenderFactory, fileFactory, io.value(), targetFileSize, | ||
| writeSchema, dsSchema); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -307,4 +319,23 @@ public WriterCommitMessage commit() throws IOException { | |
| return new TaskCommit(complete()); | ||
| } | ||
| } | ||
|
|
||
| private static class PartitionedFanout24Writer extends SparkPartitionedFanoutWriter | ||
| implements DataWriter<InternalRow> { | ||
|
|
||
| PartitionedFanout24Writer(PartitionSpec spec, FileFormat format, | ||
| SparkAppenderFactory appenderFactory, | ||
| OutputFileFactory fileFactory, FileIO fileIo, long targetFileSize, | ||
| Schema schema, StructType sparkSchema) { | ||
| super(spec, format, appenderFactory, fileFactory, fileIo, targetFileSize, schema, | ||
| sparkSchema); | ||
| } | ||
|
|
||
| @Override | ||
| public WriterCommitMessage commit() throws IOException { | ||
| close(); | ||
|
|
||
| return new TaskCommit(complete()); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.