You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by al...@apache.org on 2017/09/19 14:24:50 UTC
flink git commit: [FLINK-7630] Allow passing a File or an InputStream
to ParameterTool.fromPropertiesFile()
Repository: flink
Updated Branches:
refs/heads/master baebbabfc -> a66315a5c
[FLINK-7630] Allow passing a File or an InputStream to ParameterTool.fromPropertiesFile()
Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/a66315a5
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/a66315a5
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/a66315a5
Branch: refs/heads/master
Commit: a66315a5cc52a2596e76f09641867be6ce22242c
Parents: baebbab
Author: Tony Wei <to...@gmail.com>
Authored: Sun Sep 17 13:45:02 2017 +0800
Committer: Aljoscha Krettek <al...@gmail.com>
Committed: Tue Sep 19 16:23:27 2017 +0200
----------------------------------------------------------------------
docs/dev/best_practices.md | 8 ++++-
.../flink/api/java/utils/ParameterTool.java | 35 +++++++++++++++++---
.../flink/api/java/utils/ParameterToolTest.java | 11 ++++++
3 files changed, 48 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flink/blob/a66315a5/docs/dev/best_practices.md
----------------------------------------------------------------------
diff --git a/docs/dev/best_practices.md b/docs/dev/best_practices.md
index 6328d22..d01ea0f 100644
--- a/docs/dev/best_practices.md
+++ b/docs/dev/best_practices.md
@@ -47,8 +47,14 @@ The `ParameterTool` provides a set of predefined static methods for reading the
The following method will read a [Properties](https://docs.oracle.com/javase/tutorial/essential/environment/properties.html) file and provide the key/value pairs:
{% highlight java %}
-String propertiesFile = "/home/sam/flink/myjob.properties";
+String propertiesFilePath = "/home/sam/flink/myjob.properties";
+ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFilePath);
+
+File propertiesFile = new File(propertiesFilePath);
ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFile);
+
+InputStream propertiesFileInputStream = new FileInputStream(file);
+ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFileInputStream);
{% endhighlight %}
http://git-wip-us.apache.org/repos/asf/flink/blob/a66315a5/flink-java/src/main/java/org/apache/flink/api/java/utils/ParameterTool.java
----------------------------------------------------------------------
diff --git a/flink-java/src/main/java/org/apache/flink/api/java/utils/ParameterTool.java b/flink-java/src/main/java/org/apache/flink/api/java/utils/ParameterTool.java
index 44853f8..ef5c36e 100644
--- a/flink-java/src/main/java/org/apache/flink/api/java/utils/ParameterTool.java
+++ b/flink-java/src/main/java/org/apache/flink/api/java/utils/ParameterTool.java
@@ -33,6 +33,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Arrays;
@@ -155,13 +156,37 @@ public class ParameterTool extends ExecutionConfig.GlobalJobParameters implement
*/
public static ParameterTool fromPropertiesFile(String path) throws IOException {
File propertiesFile = new File(path);
- if (!propertiesFile.exists()) {
- throw new FileNotFoundException("Properties file " + propertiesFile.getAbsolutePath() + " does not exist");
+ return fromPropertiesFile(propertiesFile);
+ }
+
+ /**
+ * Returns {@link ParameterTool} for the given {@link Properties} file.
+ *
+ * @param file File object to the properties file
+ * @return A {@link ParameterTool}
+ * @throws IOException If the file does not exist
+ * @see Properties
+ */
+ public static ParameterTool fromPropertiesFile(File file) throws IOException {
+ if (!file.exists()) {
+ throw new FileNotFoundException("Properties file " + file.getAbsolutePath() + " does not exist");
}
- Properties props = new Properties();
- try (FileInputStream fis = new FileInputStream(propertiesFile)) {
- props.load(fis);
+ try (FileInputStream fis = new FileInputStream(file)) {
+ return fromPropertiesFile(fis);
}
+ }
+
+ /**
+ * Returns {@link ParameterTool} for the given InputStream from {@link Properties} file.
+ *
+ * @param inputStream InputStream from the properties file
+ * @return A {@link ParameterTool}
+ * @throws IOException If the file does not exist
+ * @see Properties
+ */
+ public static ParameterTool fromPropertiesFile(InputStream inputStream) throws IOException {
+ Properties props = new Properties();
+ props.load(inputStream);
return fromMap((Map) props);
}
http://git-wip-us.apache.org/repos/asf/flink/blob/a66315a5/flink-java/src/test/java/org/apache/flink/api/java/utils/ParameterToolTest.java
----------------------------------------------------------------------
diff --git a/flink-java/src/test/java/org/apache/flink/api/java/utils/ParameterToolTest.java b/flink-java/src/test/java/org/apache/flink/api/java/utils/ParameterToolTest.java
index f8549f8..d5d5c0d 100644
--- a/flink-java/src/test/java/org/apache/flink/api/java/utils/ParameterToolTest.java
+++ b/flink-java/src/test/java/org/apache/flink/api/java/utils/ParameterToolTest.java
@@ -24,6 +24,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -121,6 +122,16 @@ public class ParameterToolTest extends AbstractParameterToolTest {
ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFile.getAbsolutePath());
Assert.assertEquals(2, parameter.getNumberOfParameters());
validate(parameter);
+
+ parameter = ParameterTool.fromPropertiesFile(propertiesFile);
+ Assert.assertEquals(2, parameter.getNumberOfParameters());
+ validate(parameter);
+
+ try (FileInputStream fis = new FileInputStream(propertiesFile)) {
+ parameter = ParameterTool.fromPropertiesFile(fis);
+ }
+ Assert.assertEquals(2, parameter.getNumberOfParameters());
+ validate(parameter);
}
@Test