-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Allow custom hadoop properties to be loaded in the Spark data source #7
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
9 commits
Select commit
Hold shift + click to select a range
78f8847
Allow custom hadoop properties to be loaded in the Spark data source.
mccheah c85ce3a
Merge remote-tracking branch 'mccheah-incubator/master' into HEAD
mccheah 66f2160
Reuse Hadoop configuration in second case where it's thread-safe to d…
mccheah 46732c8
Merge remote-tracking branch 'upstream-incubator/master' into custom-…
mccheah 9e53508
Set order of precedence. Don't create too many configurations. Fix sp…
mccheah 498e58d
Merge remote-tracking branch 'mccheah-incubator/custom-hadoop-propert…
mccheah 6f07979
Properly resolve hadoop configurations by doing a second pass over op…
mccheah 8019608
Merge remote-tracking branch 'upstream-incubator/master' into custom-…
mccheah 8b238ea
Remove overwrite flag
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| import org.apache.spark.sql.types.StructType; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.netflix.iceberg.TableProperties.DEFAULT_FILE_FORMAT; | ||
|
|
@@ -56,16 +57,18 @@ public String shortName() { | |
|
|
||
| @Override | ||
| public DataSourceReader createReader(DataSourceOptions options) { | ||
| Table table = findTable(options); | ||
| return new Reader(table, lazyConf()); | ||
| Configuration conf = new Configuration(lazyBaseConf()); | ||
| Table table = getTableAndResolveHadoopConfiguration(options, conf); | ||
|
|
||
| return new Reader(table, conf); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<DataSourceWriter> createWriter(String jobId, StructType dfStruct, SaveMode mode, | ||
| DataSourceOptions options) { | ||
| Preconditions.checkArgument(mode == SaveMode.Append, "Save mode %s is not supported", mode); | ||
|
|
||
| Table table = findTable(options); | ||
| Configuration conf = new Configuration(lazyBaseConf()); | ||
| Table table = getTableAndResolveHadoopConfiguration(options, conf); | ||
|
|
||
| Schema dfSchema = SparkSchemaUtil.convert(table.schema(), dfStruct); | ||
| List<String> errors = CheckCompatibility.writeCompatibilityErrors(table.schema(), dfSchema); | ||
|
|
@@ -89,30 +92,49 @@ public Optional<DataSourceWriter> createWriter(String jobId, StructType dfStruct | |
| .toUpperCase(Locale.ENGLISH)); | ||
| } | ||
|
|
||
| return Optional.of(new Writer(table, lazyConf(), format)); | ||
| return Optional.of(new Writer(table, conf, format)); | ||
| } | ||
|
|
||
| protected Table findTable(DataSourceOptions options) { | ||
| protected Table findTable(DataSourceOptions options, Configuration conf) { | ||
| Optional<String> location = options.get("path"); | ||
| Preconditions.checkArgument(location.isPresent(), | ||
| "Cannot open table without a location: path is not set"); | ||
|
|
||
| HadoopTables tables = new HadoopTables(lazyConf()); | ||
| HadoopTables tables = new HadoopTables(conf); | ||
|
|
||
| return tables.load(location.get()); | ||
| } | ||
|
|
||
| protected SparkSession lazySparkSession() { | ||
| private SparkSession lazySparkSession() { | ||
|
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. This is nice to have in subclasses, which is why it is protected. We use it in |
||
| if (lazySpark == null) { | ||
| this.lazySpark = SparkSession.builder().getOrCreate(); | ||
| } | ||
| return lazySpark; | ||
| } | ||
|
|
||
| protected Configuration lazyConf() { | ||
| private Configuration lazyBaseConf() { | ||
| if (lazyConf == null) { | ||
| this.lazyConf = lazySparkSession().sparkContext().hadoopConfiguration(); | ||
| } | ||
| return lazyConf; | ||
| } | ||
|
|
||
| private Table getTableAndResolveHadoopConfiguration( | ||
| DataSourceOptions options, Configuration conf) { | ||
| // Overwrite configurations from the Spark Context with configurations from the options. | ||
| mergeIcebergHadoopConfs(conf, options.asMap()); | ||
| Table table = findTable(options, conf); | ||
| // Set confs from table properties | ||
| mergeIcebergHadoopConfs(conf, table.properties()); | ||
| // Re-overwrite values set in options and table properties but were not in the environment. | ||
| mergeIcebergHadoopConfs(conf, options.asMap()); | ||
| return table; | ||
| } | ||
|
|
||
| private static void mergeIcebergHadoopConfs( | ||
| Configuration baseConf, Map<String, String> options) { | ||
| options.keySet().stream() | ||
| .filter(key -> key.startsWith("iceberg.hadoop")) | ||
mccheah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .forEach(key -> baseConf.set(key.replaceFirst("iceberg.hadoop", ""), options.get(key))); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
After #47, we may not need to pass in conf. (Not something we need to change here.)