Search in sources :

Example 1 with MultipleParameterTool

use of org.apache.flink.api.java.utils.MultipleParameterTool in project flink by apache.

the class WordCount method main.

// *************************************************************************
// PROGRAM
// *************************************************************************
public static void main(String[] args) throws Exception {
    final MultipleParameterTool params = MultipleParameterTool.fromArgs(args);
    // set up the execution environment
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    // make parameters available in the web interface
    env.getConfig().setGlobalJobParameters(params);
    // get input data
    DataSet<String> text = null;
    if (params.has("input")) {
        // union all the inputs from text files
        for (String input : params.getMultiParameterRequired("input")) {
            if (text == null) {
                text = env.readTextFile(input);
            } else {
                text = text.union(env.readTextFile(input));
            }
        }
        Preconditions.checkNotNull(text, "Input DataSet should not be null.");
    } else {
        // get default test text data
        System.out.println("Executing WordCount example with default input data set.");
        System.out.println("Use --input to specify file input.");
        text = WordCountData.getDefaultTextLineDataSet(env);
    }
    DataSet<Tuple2<String, Integer>> counts = // split up the lines in pairs (2-tuples) containing: (word,1)
    text.flatMap(new Tokenizer()).groupBy(0).sum(1);
    // emit result
    if (params.has("output")) {
        counts.writeAsCsv(params.get("output"), "\n", " ");
        // execute program
        env.execute("WordCount Example");
    } else {
        System.out.println("Printing result to stdout. Use --output to specify output path.");
        counts.print();
    }
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) MultipleParameterTool(org.apache.flink.api.java.utils.MultipleParameterTool) Tuple2(org.apache.flink.api.java.tuple.Tuple2)

Example 2 with MultipleParameterTool

use of org.apache.flink.api.java.utils.MultipleParameterTool in project flink by apache.

the class CLI method fromArgs.

public static CLI fromArgs(String[] args) throws Exception {
    MultipleParameterTool params = MultipleParameterTool.fromArgs(args);
    Path[] inputs = null;
    if (params.has(INPUT_KEY)) {
        inputs = params.getMultiParameterRequired(INPUT_KEY).stream().map(Path::new).toArray(Path[]::new);
    } else {
        System.out.println("Executing example with default input data.");
        System.out.println("Use --input to specify file input.");
    }
    Path output = null;
    if (params.has(OUTPUT_KEY)) {
        output = new Path(params.get(OUTPUT_KEY));
    } else {
        System.out.println("Printing result to stdout. Use --output to specify output path.");
    }
    Duration watchInterval = null;
    if (params.has(DISCOVERY_INTERVAL)) {
        watchInterval = TimeUtils.parseDuration(params.get(DISCOVERY_INTERVAL));
    }
    RuntimeExecutionMode executionMode = ExecutionOptions.RUNTIME_MODE.defaultValue();
    if (params.has(EXECUTION_MODE)) {
        executionMode = RuntimeExecutionMode.valueOf(params.get(EXECUTION_MODE).toUpperCase());
    }
    return new CLI(inputs, output, watchInterval, executionMode, params);
}
Also used : Path(org.apache.flink.core.fs.Path) MultipleParameterTool(org.apache.flink.api.java.utils.MultipleParameterTool) Duration(java.time.Duration) RuntimeExecutionMode(org.apache.flink.api.common.RuntimeExecutionMode)

Aggregations

MultipleParameterTool (org.apache.flink.api.java.utils.MultipleParameterTool)2 Duration (java.time.Duration)1 RuntimeExecutionMode (org.apache.flink.api.common.RuntimeExecutionMode)1 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 Path (org.apache.flink.core.fs.Path)1