forked from delta-io/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DeltaConnectorConfiguration options to flink sink impl (delta-io#425
) * refactor - move options related classes from source package up to options package * Rename DeltaConfiguration to Delta, extract option validator class * move options to an internal package * fix import order * fix import order * fix test by passing default connector options * Add comments * fix comment * unused import * checkstyle fixes * checkstyle fixes * fix import order * fix import order * Add test for OptionValidator * Create an exception type for option validation errors * code review comments * Move source option validation to use common option validator * fix comment * fix indent * use junit5 api * checkstyle error Co-authored-by: Gopi Madabhushi <[email protected]>
- Loading branch information
Showing
45 changed files
with
608 additions
and
263 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...rnal/builder/BaseOptionTypeConverter.java → ...rnal/options/BaseOptionTypeConverter.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...l/builder/BooleanOptionTypeConverter.java → ...l/options/BooleanOptionTypeConverter.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...l/builder/DefaultOptionTypeConverter.java → ...l/options/DefaultOptionTypeConverter.java
This file contains 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 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
35 changes: 17 additions & 18 deletions
35
...ce/internal/DeltaSourceConfiguration.java → .../options/DeltaConnectorConfiguration.java
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
...ilder/NonNegativeNumberTypeConverter.java → ...tions/NonNegativeNumberTypeConverter.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...k/source/internal/builder/OptionType.java → ...ta/flink/internal/options/OptionType.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...internal/builder/OptionTypeConverter.java → ...internal/options/OptionTypeConverter.java
This file contains 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
127 changes: 127 additions & 0 deletions
127
flink/src/main/java/io/delta/flink/internal/options/OptionValidator.java
This file contains 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,127 @@ | ||
package io.delta.flink.internal.options; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.apache.flink.core.fs.Path; | ||
|
||
/** | ||
* Validator for delta source and sink connector configuration options. | ||
* | ||
* Setting of an option is allowed for known option names. For invalid options, the validation | ||
* throws {@link DeltaOptionValidationException}. Known option names are passed via constructor | ||
* parameter {@param validOptions}. | ||
* | ||
* This is an internal class meant for connector implementations only. | ||
* Usage example (for sink): | ||
* <code> | ||
* OptionValidator validator = new OptionValidator(sinkConfig, validSinkOptions); | ||
* validator.option("mergeSchema", true); | ||
* // For any option set on the sink, pass it to validator. If it's successful, sinkConfig | ||
* // will be updated with the corresponding option. | ||
* </code> | ||
*/ | ||
public class OptionValidator { | ||
private final Path tablePath; | ||
private final Map<String, DeltaConfigOption<?>> validOptions; | ||
private final DeltaConnectorConfiguration config; | ||
|
||
/** | ||
* Construct an option validator. | ||
* | ||
* @param tablePath Base path of the delta table. | ||
* @param config Configuration object that is populated with the validated options. | ||
* @param validOptions A map of valid options used by this instance. | ||
*/ | ||
public OptionValidator( | ||
Path tablePath, | ||
DeltaConnectorConfiguration config, | ||
Map<String, DeltaConfigOption<?>> validOptions) { | ||
this.tablePath = tablePath; | ||
this.config = config; | ||
this.validOptions = validOptions; | ||
} | ||
|
||
/** | ||
* Sets a configuration option. | ||
*/ | ||
public void option(String optionName, String optionValue) { | ||
tryToSetOption(() -> { | ||
DeltaConfigOption<?> configOption = validateOptionName(optionName); | ||
configOption.setOnConfig(config, optionValue); | ||
}); | ||
} | ||
|
||
/** | ||
* Sets a configuration option. | ||
*/ | ||
public void option(String optionName, boolean optionValue) { | ||
tryToSetOption(() -> { | ||
DeltaConfigOption<?> configOption = validateOptionName(optionName); | ||
configOption.setOnConfig(config, optionValue); | ||
}); | ||
} | ||
|
||
/** | ||
* Sets a configuration option. | ||
*/ | ||
public void option(String optionName, int optionValue) { | ||
tryToSetOption(() -> { | ||
DeltaConfigOption<?> configOption = validateOptionName(optionName); | ||
configOption.setOnConfig(config, optionValue); | ||
}); | ||
} | ||
|
||
/** | ||
* Sets a configuration option. | ||
*/ | ||
public void option(String optionName, long optionValue) { | ||
tryToSetOption(() -> { | ||
DeltaConfigOption<?> configOption = validateOptionName(optionName); | ||
configOption.setOnConfig(config, optionValue); | ||
}); | ||
} | ||
|
||
private void tryToSetOption(Executable argument) { | ||
try { | ||
argument.execute(); | ||
} catch (Exception e) { | ||
throw optionValidationException(tablePath, e); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected <TYPE> DeltaConfigOption<TYPE> validateOptionName(String optionName) { | ||
DeltaConfigOption<TYPE> option = (DeltaConfigOption<TYPE>) validOptions.get(optionName); | ||
if (option == null) { | ||
throw invalidOptionName(tablePath, optionName); | ||
} | ||
return option; | ||
} | ||
|
||
/** Exception to throw when the option name is invalid. */ | ||
private static DeltaOptionValidationException invalidOptionName( | ||
Path tablePath, | ||
String invalidOption) { | ||
return new DeltaOptionValidationException( | ||
tablePath, | ||
Collections.singletonList( | ||
String.format("Invalid option [%s] used for Delta Connector.", | ||
invalidOption))); | ||
} | ||
|
||
/** Exception to throw when there's an error while setting an option. */ | ||
private static DeltaOptionValidationException optionValidationException( | ||
Path tablePath, | ||
Exception e) { | ||
return new DeltaOptionValidationException( | ||
tablePath, | ||
Collections.singletonList(e.getClass() + " - " + e.getMessage()) | ||
); | ||
} | ||
|
||
@FunctionalInterface | ||
private interface Executable { | ||
void execute(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...r/StartingVersionOptionTypeConverter.java → ...s/StartingVersionOptionTypeConverter.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...builder/TimestampOptionTypeConverter.java → ...options/TimestampOptionTypeConverter.java
This file contains 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
Oops, something went wrong.