Search in sources :

Example 1 with InputOutputFormatContainer

use of org.apache.flink.runtime.jobgraph.InputOutputFormatContainer in project flink by apache.

the class DataSinkTask method initOutputFormat.

/**
 * Initializes the OutputFormat implementation and configuration.
 *
 * @throws RuntimeException Throws if instance of OutputFormat implementation can not be
 *     obtained.
 */
private void initOutputFormat() {
    ClassLoader userCodeClassLoader = getUserCodeClassLoader();
    // obtain task configuration (including stub parameters)
    Configuration taskConf = getTaskConfiguration();
    this.config = new TaskConfig(taskConf);
    final Pair<OperatorID, OutputFormat<IT>> operatorIDAndOutputFormat;
    InputOutputFormatContainer formatContainer = new InputOutputFormatContainer(config, userCodeClassLoader);
    try {
        operatorIDAndOutputFormat = formatContainer.getUniqueOutputFormat();
        this.format = operatorIDAndOutputFormat.getValue();
        // check if the class is a subclass, if the check is required
        if (!OutputFormat.class.isAssignableFrom(this.format.getClass())) {
            throw new RuntimeException("The class '" + this.format.getClass().getName() + "' is not a subclass of '" + OutputFormat.class.getName() + "' as is required.");
        }
    } catch (ClassCastException ccex) {
        throw new RuntimeException("The stub class is not a proper subclass of " + OutputFormat.class.getName(), ccex);
    }
    Thread thread = Thread.currentThread();
    ClassLoader original = thread.getContextClassLoader();
    // user code
    try {
        thread.setContextClassLoader(userCodeClassLoader);
        this.format.configure(formatContainer.getParameters(operatorIDAndOutputFormat.getKey()));
    } catch (Throwable t) {
        throw new RuntimeException("The user defined 'configure()' method in the Output Format caused an error: " + t.getMessage(), t);
    } finally {
        thread.setContextClassLoader(original);
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) OutputFormat(org.apache.flink.api.common.io.OutputFormat) RichOutputFormat(org.apache.flink.api.common.io.RichOutputFormat) TaskConfig(org.apache.flink.runtime.operators.util.TaskConfig) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) InputOutputFormatContainer(org.apache.flink.runtime.jobgraph.InputOutputFormatContainer)

Example 2 with InputOutputFormatContainer

use of org.apache.flink.runtime.jobgraph.InputOutputFormatContainer in project flink by apache.

the class TaskTestBase method registerFileInputTask.

public void registerFileInputTask(AbstractInvokable inTask, Class<? extends DelimitedInputFormat<Record>> stubClass, String inPath, String delimiter) {
    DelimitedInputFormat<Record> format;
    try {
        format = stubClass.newInstance();
    } catch (Throwable t) {
        throw new RuntimeException("Could not instantiate test input format.", t);
    }
    format.setFilePath(inPath);
    format.setDelimiter(delimiter);
    OperatorID operatorID = new OperatorID();
    new InputOutputFormatContainer(Thread.currentThread().getContextClassLoader()).addInputFormat(operatorID, format).write(new TaskConfig(this.mockEnv.getTaskConfiguration()));
    this.inputSplitProvider.addInputSplits(inPath, 5);
}
Also used : Record(org.apache.flink.types.Record) TaskConfig(org.apache.flink.runtime.operators.util.TaskConfig) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) InputOutputFormatContainer(org.apache.flink.runtime.jobgraph.InputOutputFormatContainer)

Example 3 with InputOutputFormatContainer

use of org.apache.flink.runtime.jobgraph.InputOutputFormatContainer in project flink by apache.

the class StreamingJobGraphGeneratorTest method testInputOutputFormat.

@Test
public void testInputOutputFormat() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStream<Long> source = env.addSource(new InputFormatSourceFunction<>(new TypeSerializerInputFormat<>(TypeInformation.of(Long.class)), TypeInformation.of(Long.class)), TypeInformation.of(Long.class)).name("source");
    source.writeUsingOutputFormat(new DiscardingOutputFormat<>()).name("sink1");
    source.writeUsingOutputFormat(new DiscardingOutputFormat<>()).name("sink2");
    StreamGraph streamGraph = env.getStreamGraph();
    JobGraph jobGraph = StreamingJobGraphGenerator.createJobGraph(streamGraph);
    assertEquals(1, jobGraph.getNumberOfVertices());
    JobVertex jobVertex = jobGraph.getVertices().iterator().next();
    assertTrue(jobVertex instanceof InputOutputFormatVertex);
    InputOutputFormatContainer formatContainer = new InputOutputFormatContainer(new TaskConfig(jobVertex.getConfiguration()), Thread.currentThread().getContextClassLoader());
    Map<OperatorID, UserCodeWrapper<? extends InputFormat<?, ?>>> inputFormats = formatContainer.getInputFormats();
    Map<OperatorID, UserCodeWrapper<? extends OutputFormat<?>>> outputFormats = formatContainer.getOutputFormats();
    assertEquals(1, inputFormats.size());
    assertEquals(2, outputFormats.size());
    Map<String, OperatorID> nameToOperatorIds = new HashMap<>();
    StreamConfig headConfig = new StreamConfig(jobVertex.getConfiguration());
    nameToOperatorIds.put(headConfig.getOperatorName(), headConfig.getOperatorID());
    Map<Integer, StreamConfig> chainedConfigs = headConfig.getTransitiveChainedTaskConfigs(Thread.currentThread().getContextClassLoader());
    for (StreamConfig config : chainedConfigs.values()) {
        nameToOperatorIds.put(config.getOperatorName(), config.getOperatorID());
    }
    InputFormat<?, ?> sourceFormat = inputFormats.get(nameToOperatorIds.get("Source: source")).getUserCodeObject();
    assertTrue(sourceFormat instanceof TypeSerializerInputFormat);
    OutputFormat<?> sinkFormat1 = outputFormats.get(nameToOperatorIds.get("Sink: sink1")).getUserCodeObject();
    assertTrue(sinkFormat1 instanceof DiscardingOutputFormat);
    OutputFormat<?> sinkFormat2 = outputFormats.get(nameToOperatorIds.get("Sink: sink2")).getUserCodeObject();
    assertTrue(sinkFormat2 instanceof DiscardingOutputFormat);
}
Also used : HashMap(java.util.HashMap) TaskConfig(org.apache.flink.runtime.operators.util.TaskConfig) InputFormatSourceFunction(org.apache.flink.streaming.api.functions.source.InputFormatSourceFunction) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) InputOutputFormatVertex(org.apache.flink.runtime.jobgraph.InputOutputFormatVertex) DiscardingOutputFormat(org.apache.flink.api.java.io.DiscardingOutputFormat) UserCodeWrapper(org.apache.flink.api.common.operators.util.UserCodeWrapper) InputOutputFormatContainer(org.apache.flink.runtime.jobgraph.InputOutputFormatContainer) DiscardingOutputFormat(org.apache.flink.api.java.io.DiscardingOutputFormat) OutputFormat(org.apache.flink.api.common.io.OutputFormat) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) JobVertex(org.apache.flink.runtime.jobgraph.JobVertex) TypeSerializerInputFormat(org.apache.flink.api.java.io.TypeSerializerInputFormat) InputFormat(org.apache.flink.api.common.io.InputFormat) TypeSerializerInputFormat(org.apache.flink.api.java.io.TypeSerializerInputFormat) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) Test(org.junit.Test)

Example 4 with InputOutputFormatContainer

use of org.apache.flink.runtime.jobgraph.InputOutputFormatContainer in project flink by apache.

the class JobGraphGenerator method createDataSinkVertex.

private JobVertex createDataSinkVertex(SinkPlanNode node) throws CompilerException {
    final InputOutputFormatVertex vertex = new InputOutputFormatVertex(node.getNodeName());
    final TaskConfig config = new TaskConfig(vertex.getConfiguration());
    final OperatorID operatorID = new OperatorID();
    vertex.setResources(node.getMinResources(), node.getPreferredResources());
    vertex.setInvokableClass(DataSinkTask.class);
    vertex.setFormatDescription(operatorID, getDescriptionForUserCode(node.getProgramOperator().getUserCodeWrapper()));
    // set user code
    new InputOutputFormatContainer(Thread.currentThread().getContextClassLoader()).addOutputFormat(operatorID, (UserCodeWrapper<? extends OutputFormat<?>>) node.getProgramOperator().getUserCodeWrapper()).addParameters(operatorID, node.getProgramOperator().getParameters()).write(config);
    return vertex;
}
Also used : TaskConfig(org.apache.flink.runtime.operators.util.TaskConfig) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) InputOutputFormatVertex(org.apache.flink.runtime.jobgraph.InputOutputFormatVertex) InputOutputFormatContainer(org.apache.flink.runtime.jobgraph.InputOutputFormatContainer)

Example 5 with InputOutputFormatContainer

use of org.apache.flink.runtime.jobgraph.InputOutputFormatContainer in project flink by apache.

the class JobGraphGenerator method createDataSourceVertex.

private JobVertex createDataSourceVertex(SourcePlanNode node) throws CompilerException {
    final InputOutputFormatVertex vertex = new InputOutputFormatVertex(node.getNodeName());
    final TaskConfig config = new TaskConfig(vertex.getConfiguration());
    final OperatorID operatorID = new OperatorID();
    vertex.setResources(node.getMinResources(), node.getPreferredResources());
    vertex.setInvokableClass(DataSourceTask.class);
    vertex.setFormatDescription(operatorID, getDescriptionForUserCode(node.getProgramOperator().getUserCodeWrapper()));
    // set user code
    new InputOutputFormatContainer(Thread.currentThread().getContextClassLoader()).addInputFormat(operatorID, (UserCodeWrapper<? extends InputFormat<?, ?>>) node.getProgramOperator().getUserCodeWrapper()).addParameters(operatorID, node.getProgramOperator().getParameters()).write(config);
    config.setOutputSerializer(node.getSerializer());
    return vertex;
}
Also used : TaskConfig(org.apache.flink.runtime.operators.util.TaskConfig) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) InputOutputFormatVertex(org.apache.flink.runtime.jobgraph.InputOutputFormatVertex) InputOutputFormatContainer(org.apache.flink.runtime.jobgraph.InputOutputFormatContainer)

Aggregations

InputOutputFormatContainer (org.apache.flink.runtime.jobgraph.InputOutputFormatContainer)7 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)7 TaskConfig (org.apache.flink.runtime.operators.util.TaskConfig)7 InputOutputFormatVertex (org.apache.flink.runtime.jobgraph.InputOutputFormatVertex)3 InputFormat (org.apache.flink.api.common.io.InputFormat)2 OutputFormat (org.apache.flink.api.common.io.OutputFormat)2 Configuration (org.apache.flink.configuration.Configuration)2 HashMap (java.util.HashMap)1 RichInputFormat (org.apache.flink.api.common.io.RichInputFormat)1 RichOutputFormat (org.apache.flink.api.common.io.RichOutputFormat)1 UserCodeWrapper (org.apache.flink.api.common.operators.util.UserCodeWrapper)1 DiscardingOutputFormat (org.apache.flink.api.java.io.DiscardingOutputFormat)1 TypeSerializerInputFormat (org.apache.flink.api.java.io.TypeSerializerInputFormat)1 Path (org.apache.flink.core.fs.Path)1 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)1 JobVertex (org.apache.flink.runtime.jobgraph.JobVertex)1 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)1 InputFormatSourceFunction (org.apache.flink.streaming.api.functions.source.InputFormatSourceFunction)1 Record (org.apache.flink.types.Record)1 UserCodeClassLoader (org.apache.flink.util.UserCodeClassLoader)1