-
Notifications
You must be signed in to change notification settings - Fork 3k
Support customizing the location where data is written in Spark #6
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 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7cc01b9
Support customizing the location where data is written in Spark.
mccheah f158e1b
Merge remote-tracking branch 'mccheah-incubator/master' into HEAD
mccheah dcc3404
Support customizing the location where data is written in Spark.
mccheah 3e88958
Minor comments
mccheah 798cfb4
Adjust tests.
mccheah f917697
Test various write locations
mccheah f48a2f0
Merge remote-tracking branch 'upstream-incubator/master' into custom-…
mccheah 16efff2
Don't allow write data location to be set in data source options.
mccheah 00368db
Merge remote-tracking branch 'mccheah-incubator/custom-data-location'…
mccheah 9755b09
Address some comments
mccheah a651062
Don't import
mccheah e415c83
Make test less convoluted
mccheah cc1f33f
Remove duplicate tests
mccheah 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,21 +32,25 @@ | |
| import com.netflix.iceberg.spark.data.AvroDataTest; | ||
| import com.netflix.iceberg.spark.data.RandomData; | ||
| import com.netflix.iceberg.spark.data.SparkAvroReader; | ||
| import com.netflix.iceberg.types.Types; | ||
| import org.apache.avro.generic.GenericData.Record; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.spark.api.java.JavaRDD; | ||
| import org.apache.spark.api.java.JavaSparkContext; | ||
| import org.apache.spark.sql.DataFrameWriter; | ||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
| import org.apache.spark.sql.SparkSession; | ||
| import org.apache.spark.sql.catalyst.InternalRow; | ||
| import org.junit.AfterClass; | ||
| import org.junit.Assert; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.util.List; | ||
|
|
||
| import static com.netflix.iceberg.spark.SparkSchemaUtil.convert; | ||
|
|
@@ -56,6 +60,9 @@ | |
| @RunWith(Parameterized.class) | ||
| public class TestDataFrameWrites extends AvroDataTest { | ||
| private static final Configuration CONF = new Configuration(); | ||
| private static final Schema BASIC_SCHEMA = new Schema( | ||
| Types.NestedField.required(0, "id", Types.LongType.get()), | ||
| Types.NestedField.optional(1, "data", Types.ListType.ofOptional(2, Types.StringType.get()))); | ||
|
|
||
| private String format = null; | ||
|
|
||
|
|
@@ -91,23 +98,55 @@ public static void stopSpark() { | |
|
|
||
| @Override | ||
| protected void writeAndValidate(Schema schema) throws IOException { | ||
| writeAndValidateWithLocations(schema, false, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWrite_overridingDataLocation_tablePropertyOnly() throws IOException { | ||
| writeAndValidateWithLocations(BASIC_SCHEMA, true, false); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWrite_overridingDataLocation_sourceOptionOnly() throws IOException { | ||
| writeAndValidateWithLocations(BASIC_SCHEMA, false, true); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWrite_overridingDataLocation_sourceOptionTakesPrecedence() throws IOException { | ||
| writeAndValidateWithLocations(BASIC_SCHEMA, true, true); | ||
| } | ||
|
|
||
| private void writeAndValidateWithLocations( | ||
| Schema schema, | ||
| boolean setTablePropertyDataLocation, | ||
|
||
| boolean setWriterOptionDataLocation) throws IOException { | ||
| File parent = temp.newFolder("parquet"); | ||
| File location = new File(parent, "test"); | ||
| Assert.assertTrue("Mkdir should succeed", location.mkdirs()); | ||
|
|
||
| File tablePropertyDataLocation = new File(parent, "test-table-property-data-dir"); | ||
| Assert.assertTrue("Mkdir should succeed", tablePropertyDataLocation.mkdirs()); | ||
| File writerPropertyDataLocation = new File(parent, "test-source-option-data-dir"); | ||
| Assert.assertTrue("Mkdir should succeed", writerPropertyDataLocation.mkdirs()); | ||
|
|
||
| HadoopTables tables = new HadoopTables(CONF); | ||
| Table table = tables.create(schema, PartitionSpec.unpartitioned(), location.toString()); | ||
| Schema tableSchema = table.schema(); // use the table schema because ids are reassigned | ||
|
|
||
| table.updateProperties().set(TableProperties.DEFAULT_FILE_FORMAT, format).commit(); | ||
| if (setTablePropertyDataLocation) { | ||
| table.updateProperties().set( | ||
| TableProperties.WRITE_NEW_DATA_LOCATION, tablePropertyDataLocation.getAbsolutePath()).commit(); | ||
| } | ||
|
|
||
| List<Record> expected = RandomData.generateList(tableSchema, 100, 0L); | ||
| Dataset<Row> df = createDataset(expected, tableSchema); | ||
| DataFrameWriter<?> writer = df.write().format("iceberg").mode("append"); | ||
| if (setWriterOptionDataLocation) { | ||
| writer = writer.option(TableProperties.WRITE_NEW_DATA_LOCATION, writerPropertyDataLocation.getAbsolutePath()); | ||
| } | ||
|
|
||
| df.write() | ||
| .format("iceberg") | ||
| .mode("append") | ||
| .save(location.toString()); | ||
| writer.save(location.toString()); | ||
|
|
||
| table.refresh(); | ||
|
|
||
|
|
@@ -121,6 +160,22 @@ protected void writeAndValidate(Schema schema) throws IOException { | |
| for (int i = 0; i < expected.size(); i += 1) { | ||
| assertEqualsSafe(tableSchema.asStruct(), expected.get(i), actual.get(i)); | ||
| } | ||
|
|
||
| File expectedDataDir; | ||
| if (setWriterOptionDataLocation) { | ||
| expectedDataDir = writerPropertyDataLocation; | ||
| } else if (setTablePropertyDataLocation) { | ||
| expectedDataDir = tablePropertyDataLocation; | ||
| } else { | ||
| expectedDataDir = new File(location, "data"); | ||
| } | ||
| table.currentSnapshot().addedFiles().forEach(dataFile -> | ||
| Assert.assertTrue( | ||
| String.format( | ||
| "File should have the parent directory %s, but has: %s.", | ||
| expectedDataDir.getAbsolutePath(), | ||
| dataFile.path()), | ||
| URI.create(dataFile.path().toString()).getPath().startsWith(expectedDataDir.getAbsolutePath()))); | ||
| } | ||
|
|
||
| private Dataset<Row> createDataset(List<Record> records, Schema schema) throws IOException { | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding parameters to Writer whenever a change like this is made, I'd rather pass the options into Writer and handle these there. The
dataLocationmethod could do this work instead of moving it outside the Writer class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think doing options processing from a
Map<String, String>, inside a constructor, is a bit of an antipattern. Consider for example writing a unit test for this class in the future. If we pass theWriterconstructor only aHashMap, the unit test would have to construct thatHashMapin a specific way, i.e. knowing what key-value pairs the constructor is expecting.Perhaps we can have a builder object that acts as a factory that accepts the
Mapand returns theWriter. TheWriterconstructor accepts the builder object and copies the set fields on the builder into its own fields.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I think is strange is passing the location of a write into the writer when we're passing table into the writer. Why isn't that logic entirely handled in the writer? The normal case is for the write location to come from table config. I'm not even sure that we should allow overriding the write location in Spark's write properties. What is the use case there?
I like your reasoning about not passing options as a map to make testing clear in general, but doing it here just shifts the concern to a different test. The test case is that setting "write.folder-storage.path" in Spark options changes the location of output files. A test that passes in the location can validate that the location is respected, but what we actually want to do is test that the table's location defaults, or is set by the table property, or (maybe) is set by Spark options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for our use case we can have the write location specified in the table property. That would be sufficient. I also don't see the downside of introducing the extra flexibility of allowing the override to be specified in data source options, but we could defer the feature until later.