flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (FLINK-2021) Rework examples to use ParameterTool
Date Thu, 21 Jan 2016 13:52:39 GMT

    [ https://issues.apache.org/jira/browse/FLINK-2021?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15110609#comment-15110609
] 

ASF GitHub Bot commented on FLINK-2021:
---------------------------------------

Github user stefanobaghino commented on a diff in the pull request:

    https://github.com/apache/flink/pull/1536#discussion_r50402286
  
    --- Diff: flink-examples/flink-examples-batch/src/main/java/org/apache/flink/examples/java/clustering/KMeans.java
---
    @@ -291,31 +297,54 @@ public Centroid map(Tuple3<Integer, Point, Long> value) {
     	private static String centersPath = null;
     	private static String outputPath = null;
     	private static int numIterations = 10;
    -	
    -	private static boolean parseParameters(String[] programArguments) {
    -		
    -		if(programArguments.length > 0) {
    -			// parse input arguments
    +
    +	private static final Option POINTS_PATH_OPTION =
    +		new Option("points").alt("P").help("The path to the input points");
    +	private static final Option CENTERS_PATH_OPTION =
    +		new Option("centroids").alt("C").help("The path to the input centroids");
    +	private static final Option OUTPUT_PATH_OPTION =
    +		new Option("output").alt("O").help("The path where the output will be written");
    +	private static final Option NUM_ITERATIONS_OPTION =
    +		new Option("iterations").alt("I").help("The number of iteration performed by the K-Means
algorithm");
    +
    +	private static boolean parseParameters(final ParameterTool params) throws RequiredParametersException
{
    +
    +		final RequiredParameters requiredParameters = new RequiredParameters();
    +		boolean parseStatus = false;
    +
    +		requiredParameters.add(POINTS_PATH_OPTION);
    +		requiredParameters.add(CENTERS_PATH_OPTION);
    +		requiredParameters.add(OUTPUT_PATH_OPTION);
    +		requiredParameters.add(NUM_ITERATIONS_OPTION);
    +
    +		try {
    +			requiredParameters.applyTo(params);
    +			pointsPath = params.get(POINTS_PATH_OPTION.getName());
    +			centersPath = params.get(CENTERS_PATH_OPTION.getName());
    +			outputPath = params.get(OUTPUT_PATH_OPTION.getName());
    +			numIterations = params.getInt(NUM_ITERATIONS_OPTION.getName());
     			fileOutput = true;
    -			if(programArguments.length == 4) {
    -				pointsPath = programArguments[0];
    -				centersPath = programArguments[1];
    -				outputPath = programArguments[2];
    -				numIterations = Integer.parseInt(programArguments[3]);
    +			parseStatus = true;
    +		} catch (RequiredParametersException e) {
    +			if (params.getNumberOfParameters() == 0) {
    +				printRunWithDefaultParams();
    +				parseStatus = true;
     			} else {
    -				System.err.println("Usage: KMeans <points path> <centers path> <result
path> <num iterations>");
    -				return false;
    +				System.out.println(requiredParameters.getHelp(e.getMissingArguments()));
     			}
    -		} else {
    -			System.out.println("Executing K-Means example with default parameters and built-in
default data.");
    -			System.out.println("  Provide parameters to read input data from files.");
    -			System.out.println("  See the documentation for the correct format of input files.");
    -			System.out.println("  We provide a data generator to create synthetic input files
for this program.");
    -			System.out.println("  Usage: KMeans <points path> <centers path> <result
path> <num iterations>");
     		}
    -		return true;
    +
    +		return parseStatus;
     	}
    -	
    +
    +	private static void printRunWithDefaultParams() {
    +		System.out.println("Executing K-Means example with default parameters and built-in
default data.");
    +		System.out.println("  Provide parameters to read input data from files.");
    +		System.out.println("  See the documentation for the correct format of input files.");
    +		System.out.println("  We provide a data generator to create synthetic input files for
this program.");
    +		System.out.println("  Usage: KMeans --points <points path> --centroids <centers
path> --output <result path> --iterations <num iterations>");
    --- End diff --
    
    Clever! I'll do it, thanks!


> Rework examples to use ParameterTool
> ------------------------------------
>
>                 Key: FLINK-2021
>                 URL: https://issues.apache.org/jira/browse/FLINK-2021
>             Project: Flink
>          Issue Type: Improvement
>          Components: Examples
>    Affects Versions: 0.9
>            Reporter: Robert Metzger
>            Priority: Minor
>              Labels: starter
>
> In FLINK-1525, we introduced the {{ParameterTool}}.
> We should port the examples to use the tool.
> The examples could look like this (we should maybe discuss it first on the mailing lists):
> {code}
> public static void main(String[] args) throws Exception {
>     ParameterTool pt = ParameterTool.fromArgs(args);
>     boolean fileOutput = pt.getNumberOfParameters() == 2;
>     String textPath = null;
>     String outputPath = null;
>     if(fileOutput) {
>         textPath = pt.getRequired("input");
>         outputPath = pt.getRequired("output");
>     }
>     // set up the execution environment
>     final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
>     env.getConfig().setUserConfig(pt);
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message