Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 45657200CF7 for ; Tue, 19 Sep 2017 17:04:13 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 4411A1609DD; Tue, 19 Sep 2017 15:04:13 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 614CF1609DB for ; Tue, 19 Sep 2017 17:04:12 +0200 (CEST) Received: (qmail 3362 invoked by uid 500); 19 Sep 2017 15:04:11 -0000 Mailing-List: contact commits-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list commits@flink.apache.org Received: (qmail 3353 invoked by uid 99); 19 Sep 2017 15:04:11 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 19 Sep 2017 15:04:11 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6FA23F565D; Tue, 19 Sep 2017 15:04:11 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: aljoscha@apache.org To: commits@flink.apache.org Message-Id: <72e01d9ccf164d3f9caba83c286de1cf@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: flink git commit: [FLINK-7630] Allow passing a File or an InputStream to ParameterTool.fromPropertiesFile() Date: Tue, 19 Sep 2017 15:04:11 +0000 (UTC) archived-at: Tue, 19 Sep 2017 15:04:13 -0000 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 Authored: Sun Sep 17 13:45:02 2017 +0800 Committer: Aljoscha Krettek 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