forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-12787][python] Allow to specify directory in option -pyfs
This closes apache#8671
- Loading branch information
1 parent
a21cc24
commit 74ef636
Showing
8 changed files
with
405 additions
and
182 deletions.
There are no files selected for viewing
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
63 changes: 63 additions & 0 deletions
63
flink-python/src/main/java/org/apache/flink/python/client/PythonDriverOptions.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,63 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.flink.python.client; | ||
|
||
import org.apache.flink.core.fs.Path; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Options for the {@link PythonDriver}. | ||
*/ | ||
final class PythonDriverOptions { | ||
|
||
@Nonnull | ||
private String entrypointModule; | ||
|
||
@Nonnull | ||
private List<Path> pythonLibFiles; | ||
|
||
@Nonnull | ||
private List<String> programArgs; | ||
|
||
PythonDriverOptions(String entrypointModule, List<Path> pythonLibFiles, List<String> programArgs) { | ||
this.entrypointModule = requireNonNull(entrypointModule, "entrypointModule"); | ||
this.pythonLibFiles = requireNonNull(pythonLibFiles, "pythonLibFiles"); | ||
this.programArgs = requireNonNull(programArgs, "programArgs"); | ||
} | ||
|
||
@Nonnull | ||
String getEntrypointModule() { | ||
return entrypointModule; | ||
} | ||
|
||
@Nonnull | ||
List<Path> getPythonLibFiles() { | ||
return pythonLibFiles; | ||
} | ||
|
||
@Nonnull | ||
List<String> getProgramArgs() { | ||
return programArgs; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...python/src/main/java/org/apache/flink/python/client/PythonDriverOptionsParserFactory.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,112 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.flink.python.client; | ||
|
||
import org.apache.flink.core.fs.Path; | ||
import org.apache.flink.runtime.entrypoint.FlinkParseException; | ||
import org.apache.flink.runtime.entrypoint.parser.ParserResultFactory; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Parser factory which generates a {@link PythonDriverOptions} from a given | ||
* list of command line arguments. | ||
*/ | ||
final class PythonDriverOptionsParserFactory implements ParserResultFactory<PythonDriverOptions> { | ||
|
||
private static final Option PY_OPTION = Option.builder("py") | ||
.longOpt("python") | ||
.required(false) | ||
.hasArg(true) | ||
.argName("entrypoint python file") | ||
.desc("Python script with the program entry point. " + | ||
"The dependent resources can be configured with the `--pyFiles` option.") | ||
.build(); | ||
|
||
private static final Option PYMODULE_OPTION = Option.builder("pym") | ||
.longOpt("pyModule") | ||
.required(false) | ||
.hasArg(true) | ||
.argName("entrypoint module name") | ||
.desc("Python module with the program entry point. " + | ||
"This option must be used in conjunction with `--pyFiles`.") | ||
.build(); | ||
|
||
private static final Option PYFILES_OPTION = Option.builder("pyfs") | ||
.longOpt("pyFiles") | ||
.required(false) | ||
.hasArg(true) | ||
.argName("entrypoint python file") | ||
.desc("Attach custom python files for job. " + | ||
"Comma can be used as the separator to specify multiple files. " + | ||
"The standard python resource file suffixes such as .py/.egg/.zip are all supported." + | ||
"(eg: --pyFiles file:https:///tmp/myresource.zip,hdfs:https:///$namenode_address/myresource2.zip)") | ||
.build(); | ||
|
||
@Override | ||
public Options getOptions() { | ||
final Options options = new Options(); | ||
options.addOption(PY_OPTION); | ||
options.addOption(PYMODULE_OPTION); | ||
options.addOption(PYFILES_OPTION); | ||
return options; | ||
} | ||
|
||
@Override | ||
public PythonDriverOptions createResult(@Nonnull CommandLine commandLine) throws FlinkParseException { | ||
String entrypointModule = null; | ||
final List<Path> pythonLibFiles = new ArrayList<>(); | ||
|
||
if (commandLine.hasOption(PY_OPTION.getOpt()) && commandLine.hasOption(PYMODULE_OPTION.getOpt())) { | ||
throw new FlinkParseException("Cannot use options -py and -pym simultaneously."); | ||
} else if (commandLine.hasOption(PY_OPTION.getOpt())) { | ||
Path file = new Path(commandLine.getOptionValue(PY_OPTION.getOpt())); | ||
String fileName = file.getName(); | ||
if (fileName.endsWith(".py")) { | ||
entrypointModule = fileName.substring(0, fileName.length() - 3); | ||
pythonLibFiles.add(file); | ||
} | ||
} else if (commandLine.hasOption(PYMODULE_OPTION.getOpt())) { | ||
entrypointModule = commandLine.getOptionValue(PYMODULE_OPTION.getOpt()); | ||
} else { | ||
throw new FlinkParseException( | ||
"The Python entrypoint has not been specified. It can be specified with options -py or -pym"); | ||
} | ||
|
||
if (commandLine.hasOption(PYFILES_OPTION.getOpt())) { | ||
pythonLibFiles.addAll( | ||
Arrays.stream(commandLine.getOptionValue(PYFILES_OPTION.getOpt()).split(",")) | ||
.map(Path::new) | ||
.collect(Collectors.toCollection(ArrayList::new))); | ||
} else if (commandLine.hasOption(PYMODULE_OPTION.getOpt())) { | ||
throw new FlinkParseException("Option -pym must be used in conjunction with `--pyFiles`"); | ||
} | ||
|
||
return new PythonDriverOptions(entrypointModule, pythonLibFiles, commandLine.getArgList()); | ||
} | ||
} |
Oops, something went wrong.