Skip to content

Commit a66315a

Browse files
tony810430aljoscha
authored andcommitted
[FLINK-7630] Allow passing a File or an InputStream to ParameterTool.fromPropertiesFile()
1 parent baebbab commit a66315a

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

docs/dev/best_practices.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ The `ParameterTool` provides a set of predefined static methods for reading the
4747

4848
The following method will read a [Properties](https://docs.oracle.com/javase/tutorial/essential/environment/properties.html) file and provide the key/value pairs:
4949
{% highlight java %}
50-
String propertiesFile = "/home/sam/flink/myjob.properties";
50+
String propertiesFilePath = "/home/sam/flink/myjob.properties";
51+
ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFilePath);
52+
53+
File propertiesFile = new File(propertiesFilePath);
5154
ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFile);
55+
56+
InputStream propertiesFileInputStream = new FileInputStream(file);
57+
ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFileInputStream);
5258
{% endhighlight %}
5359

5460

flink-java/src/main/java/org/apache/flink/api/java/utils/ParameterTool.java

+30-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.io.FileNotFoundException;
3434
import java.io.FileOutputStream;
3535
import java.io.IOException;
36+
import java.io.InputStream;
3637
import java.io.OutputStream;
3738
import java.io.Serializable;
3839
import java.util.Arrays;
@@ -155,13 +156,37 @@ else if (arg.startsWith("-")) {
155156
*/
156157
public static ParameterTool fromPropertiesFile(String path) throws IOException {
157158
File propertiesFile = new File(path);
158-
if (!propertiesFile.exists()) {
159-
throw new FileNotFoundException("Properties file " + propertiesFile.getAbsolutePath() + " does not exist");
159+
return fromPropertiesFile(propertiesFile);
160+
}
161+
162+
/**
163+
* Returns {@link ParameterTool} for the given {@link Properties} file.
164+
*
165+
* @param file File object to the properties file
166+
* @return A {@link ParameterTool}
167+
* @throws IOException If the file does not exist
168+
* @see Properties
169+
*/
170+
public static ParameterTool fromPropertiesFile(File file) throws IOException {
171+
if (!file.exists()) {
172+
throw new FileNotFoundException("Properties file " + file.getAbsolutePath() + " does not exist");
160173
}
161-
Properties props = new Properties();
162-
try (FileInputStream fis = new FileInputStream(propertiesFile)) {
163-
props.load(fis);
174+
try (FileInputStream fis = new FileInputStream(file)) {
175+
return fromPropertiesFile(fis);
164176
}
177+
}
178+
179+
/**
180+
* Returns {@link ParameterTool} for the given InputStream from {@link Properties} file.
181+
*
182+
* @param inputStream InputStream from the properties file
183+
* @return A {@link ParameterTool}
184+
* @throws IOException If the file does not exist
185+
* @see Properties
186+
*/
187+
public static ParameterTool fromPropertiesFile(InputStream inputStream) throws IOException {
188+
Properties props = new Properties();
189+
props.load(inputStream);
165190
return fromMap((Map) props);
166191
}
167192

flink-java/src/test/java/org/apache/flink/api/java/utils/ParameterToolTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.rules.ExpectedException;
2525

2626
import java.io.File;
27+
import java.io.FileInputStream;
2728
import java.io.FileOutputStream;
2829
import java.io.IOException;
2930
import java.io.OutputStream;
@@ -121,6 +122,16 @@ public void testFromPropertiesFile() throws IOException {
121122
ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFile.getAbsolutePath());
122123
Assert.assertEquals(2, parameter.getNumberOfParameters());
123124
validate(parameter);
125+
126+
parameter = ParameterTool.fromPropertiesFile(propertiesFile);
127+
Assert.assertEquals(2, parameter.getNumberOfParameters());
128+
validate(parameter);
129+
130+
try (FileInputStream fis = new FileInputStream(propertiesFile)) {
131+
parameter = ParameterTool.fromPropertiesFile(fis);
132+
}
133+
Assert.assertEquals(2, parameter.getNumberOfParameters());
134+
validate(parameter);
124135
}
125136

126137
@Test

0 commit comments

Comments
 (0)