flink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aljos...@apache.org
Subject flink git commit: [FLINK-7630] Allow passing a File or an InputStream to ParameterTool.fromPropertiesFile()
Date Tue, 19 Sep 2017 15:04:11 GMT
Repository: flink
Updated Branches:
  refs/heads/release-1.3 b63955ed9 -> 100951e27


[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/100951e2
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/100951e2
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/100951e2

Branch: refs/heads/release-1.3
Commit: 100951e279ad352f3b8921970217cd8edf14ac20
Parents: b63955e
Author: Tony Wei <tony19920430@gmail.com>
Authored: Sun Sep 17 13:45:02 2017 +0800
Committer: Aljoscha Krettek <aljoscha.krettek@gmail.com>
Committed: Tue Sep 19 16:27:37 2017 +0200

----------------------------------------------------------------------
 docs/dev/best_practices.md                      |  8 ++++-
 .../flink/api/java/utils/ParameterTool.java     | 37 ++++++++++++++++----
 .../flink/api/java/utils/ParameterToolTest.java | 11 ++++++
 3 files changed, 49 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/100951e2/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/100951e2/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 8e15441..1fcfb9b 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
@@ -31,6 +31,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;
@@ -152,14 +153,38 @@ 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);
 		}
-		return fromMap((Map)props);
+	}
+
+	/**
+	 * 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/100951e2/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 9b63985..a36d046 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
@@ -22,6 +22,7 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -117,6 +118,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


Mime
View raw message