Since Flink 0.9, we have a built-in ParameterTool
in Flink, which helps to get parameters from external sources such as arguments, system properties, or from property files. Internally, it is a map of strings which keeps the key as the parameter name and the value as the parameter value.
For example, we can think of using ParameterTool in our DataStream API example, where we need to set Kafka properties:
String kafkaproperties = "/path/to/kafka.properties"; ParameterTool parameter = ParameterTool.fromPropertiesFile(propertiesFile);
We can read properties defined in system variables. We need to pass the system properties file before initializing them by setting Dinput=hdfs://myfile
.
Now we can read all those properties in ParameterTool
as follows:
ParameterTool parameters = ParameterTool.fromSystemProperties();
We can also read the parameters from command line arguments. We have to set --elements
before invoking the application.
The following code shows how to read parameters from command line arguments:
ParameterTool parameters = ParameterTool.fromArgs(args);
We can also read the parameters from the .properties
file. The following is the code for this:
String propertiesFile = /my.properties"; ParameterTool parameters = ParameterTool.fromPropertiesFile(propertiesFile);
We can read the parameters in the Flink program. The following shows how we get the parameters:
parameter.getRequired("key"); parameter.get("paramterName", "myDefaultValue"); parameter.getLong("expectedCount", -1L); parameter.getNumberOfParameters()
3.145.75.217