Search in sources :

Example 1 with Input

use of org.apache.flink.graph.drivers.input.Input in project flink by apache.

the class Runner method run.

/**
 * Setup the Flink job with the graph input, algorithm, and output.
 *
 * <p>To then execute the job call {@link #execute}.
 *
 * @return this
 * @throws Exception on error
 */
public Runner run() throws Exception {
    // Set up the execution environment
    env = ExecutionEnvironment.getExecutionEnvironment();
    ExecutionConfig config = env.getConfig();
    // should not have any non-Flink data types
    config.disableForceAvro();
    config.disableForceKryo();
    config.setGlobalJobParameters(parameters);
    parameterize(this);
    // configure local parameters and throw proper exception on error
    try {
        this.configure(parameters);
    } catch (RuntimeException ex) {
        throw new ProgramParametrizationException(ex.getMessage());
    }
    // integration tests run with with object reuse both disabled and enabled
    if (disableObjectReuse.getValue()) {
        config.disableObjectReuse();
    } else {
        config.enableObjectReuse();
    }
    // algorithm and usage
    if (!parameters.has(ALGORITHM)) {
        throw new ProgramParametrizationException(getAlgorithmsListing());
    }
    String algorithmName = parameters.get(ALGORITHM);
    algorithm = driverFactory.get(algorithmName);
    if (algorithm == null) {
        throw new ProgramParametrizationException("Unknown algorithm name: " + algorithmName);
    }
    // input and usage
    if (!parameters.has(INPUT)) {
        if (!parameters.has(OUTPUT)) {
            // if neither input nor output is given then print algorithm usage
            throw new ProgramParametrizationException(getAlgorithmUsage(algorithmName));
        }
        throw new ProgramParametrizationException("No input given");
    }
    parameterize(algorithm);
    String inputName = parameters.get(INPUT);
    Input input = inputFactory.get(inputName);
    if (input == null) {
        throw new ProgramParametrizationException("Unknown input type: " + inputName);
    }
    parameterize(input);
    // output and usage
    if (!parameters.has(OUTPUT)) {
        throw new ProgramParametrizationException("No output given");
    }
    String outputName = parameters.get(OUTPUT);
    output = outputFactory.get(outputName);
    if (output == null) {
        throw new ProgramParametrizationException("Unknown output type: " + outputName);
    }
    parameterize(output);
    // ----------------------------------------------------------------------------------------
    // Create list of input and algorithm transforms
    // ----------------------------------------------------------------------------------------
    List<Transform> transforms = new ArrayList<>();
    if (input instanceof Transformable) {
        transforms.addAll(((Transformable) input).getTransformers());
    }
    if (algorithm instanceof Transformable) {
        transforms.addAll(((Transformable) algorithm).getTransformers());
    }
    for (Transform transform : transforms) {
        parameterize(transform);
    }
    // unused parameters
    if (parameters.getUnrequestedParameters().size() > 0) {
        throw new ProgramParametrizationException("Unrequested parameters: " + parameters.getUnrequestedParameters());
    }
    // ----------------------------------------------------------------------------------------
    // Execute
    // ----------------------------------------------------------------------------------------
    // Create input
    Graph graph = input.create(env);
    // Transform input
    for (Transform transform : transforms) {
        graph = (Graph) transform.transformInput(graph);
    }
    // Run algorithm
    result = algorithm.plan(graph);
    // Output
    executionName = jobName.getValue() != null ? jobName.getValue() + ": " : "";
    executionName += input.getIdentity() + " ⇨ " + algorithmName + " ⇨ " + output.getName();
    if (transforms.size() > 0) {
        // append identifiers to job name
        StringBuffer buffer = new StringBuffer(executionName).append(" [");
        for (Transform transform : transforms) {
            buffer.append(transform.getIdentity());
        }
        executionName = buffer.append("]").toString();
    }
    if (output == null) {
        throw new ProgramParametrizationException("Unknown output type: " + outputName);
    }
    try {
        output.configure(parameters);
    } catch (RuntimeException ex) {
        throw new ProgramParametrizationException(ex.getMessage());
    }
    if (result != null) {
        // Transform output if algorithm returned result DataSet
        if (transforms.size() > 0) {
            Collections.reverse(transforms);
            for (Transform transform : transforms) {
                result = (DataSet) transform.transformResult(result);
            }
        }
    }
    return this;
}
Also used : Input(org.apache.flink.graph.drivers.input.Input) CycleGraph(org.apache.flink.graph.drivers.input.CycleGraph) CompleteGraph(org.apache.flink.graph.drivers.input.CompleteGraph) SingletonEdgeGraph(org.apache.flink.graph.drivers.input.SingletonEdgeGraph) RMatGraph(org.apache.flink.graph.drivers.input.RMatGraph) StarGraph(org.apache.flink.graph.drivers.input.StarGraph) CirculantGraph(org.apache.flink.graph.drivers.input.CirculantGraph) EmptyGraph(org.apache.flink.graph.drivers.input.EmptyGraph) PathGraph(org.apache.flink.graph.drivers.input.PathGraph) EchoGraph(org.apache.flink.graph.drivers.input.EchoGraph) HypercubeGraph(org.apache.flink.graph.drivers.input.HypercubeGraph) GridGraph(org.apache.flink.graph.drivers.input.GridGraph) ProgramParametrizationException(org.apache.flink.client.program.ProgramParametrizationException) ArrayList(java.util.ArrayList) Transformable(org.apache.flink.graph.drivers.transform.Transformable) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Transform(org.apache.flink.graph.drivers.transform.Transform)

Example 2 with Input

use of org.apache.flink.graph.drivers.input.Input in project flink by apache.

the class Runner method getAlgorithmUsage.

/**
 * Display the usage for the given algorithm. This includes options for all compatible inputs,
 * the selected algorithm, and outputs implemented by the selected algorithm.
 *
 * @param algorithmName unique identifier of the selected algorithm
 * @return usage string for the given algorithm
 */
private static String getAlgorithmUsage(String algorithmName) {
    StrBuilder strBuilder = new StrBuilder();
    Driver algorithm = driverFactory.get(algorithmName);
    strBuilder.appendNewLine().appendNewLine().appendln(algorithm.getLongDescription()).appendNewLine().append("usage: flink run examples/flink-gelly-examples_<version>.jar --algorithm ").append(algorithmName).append(" [algorithm options] --input <input> [input options] --output <output> [output options]").appendNewLine().appendNewLine().appendln("Available inputs:");
    for (Input input : inputFactory) {
        strBuilder.append("  --input ").append(input.getName()).append(" ").appendln(input.getUsage());
    }
    String algorithmParameterization = algorithm.getUsage();
    if (algorithmParameterization.length() > 0) {
        strBuilder.appendNewLine().appendln("Algorithm configuration:").append("  ").appendln(algorithm.getUsage());
    }
    strBuilder.appendNewLine().appendln("Available outputs:");
    for (Output output : outputFactory) {
        strBuilder.append("  --output ").append(output.getName()).append(" ").appendln(output.getUsage());
    }
    return strBuilder.appendNewLine().toString();
}
Also used : Input(org.apache.flink.graph.drivers.input.Input) Output(org.apache.flink.graph.drivers.output.Output) Driver(org.apache.flink.graph.drivers.Driver) StrBuilder(org.apache.commons.lang3.text.StrBuilder)

Aggregations

Input (org.apache.flink.graph.drivers.input.Input)2 ArrayList (java.util.ArrayList)1 StrBuilder (org.apache.commons.lang3.text.StrBuilder)1 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 ProgramParametrizationException (org.apache.flink.client.program.ProgramParametrizationException)1 Driver (org.apache.flink.graph.drivers.Driver)1 CirculantGraph (org.apache.flink.graph.drivers.input.CirculantGraph)1 CompleteGraph (org.apache.flink.graph.drivers.input.CompleteGraph)1 CycleGraph (org.apache.flink.graph.drivers.input.CycleGraph)1 EchoGraph (org.apache.flink.graph.drivers.input.EchoGraph)1 EmptyGraph (org.apache.flink.graph.drivers.input.EmptyGraph)1 GridGraph (org.apache.flink.graph.drivers.input.GridGraph)1 HypercubeGraph (org.apache.flink.graph.drivers.input.HypercubeGraph)1 PathGraph (org.apache.flink.graph.drivers.input.PathGraph)1 RMatGraph (org.apache.flink.graph.drivers.input.RMatGraph)1 SingletonEdgeGraph (org.apache.flink.graph.drivers.input.SingletonEdgeGraph)1 StarGraph (org.apache.flink.graph.drivers.input.StarGraph)1 Output (org.apache.flink.graph.drivers.output.Output)1 Transform (org.apache.flink.graph.drivers.transform.Transform)1 Transformable (org.apache.flink.graph.drivers.transform.Transformable)1