Search in sources :

Example 1 with FromElementsFunction

use of org.apache.flink.streaming.api.functions.source.FromElementsFunction in project flink by apache.

the class SourceStreamTaskTest method testClosingAllOperatorsOnChainProperly.

@Test
public void testClosingAllOperatorsOnChainProperly() throws Exception {
    final StreamTaskTestHarness<String> testHarness = new StreamTaskTestHarness<>(SourceStreamTask::new, STRING_TYPE_INFO);
    testHarness.setupOperatorChain(new OperatorID(), new OutputRecordInCloseTestSource<>("Source0", new FromElementsFunction<>(StringSerializer.INSTANCE, "Hello"))).chain(new OperatorID(), new TestBoundedOneInputStreamOperator("Operator1"), STRING_TYPE_INFO.createSerializer(new ExecutionConfig())).finish();
    StreamConfig streamConfig = testHarness.getStreamConfig();
    streamConfig.setTimeCharacteristic(TimeCharacteristic.ProcessingTime);
    testHarness.invoke();
    testHarness.waitForTaskCompletion();
    ArrayList<Object> expected = new ArrayList<>();
    Collections.addAll(expected, new StreamRecord<>("Hello"), new StreamRecord<>("[Source0]: End of input"), Watermark.MAX_WATERMARK, new StreamRecord<>("[Source0]: Finish"), new StreamRecord<>("[Operator1]: End of input"), new StreamRecord<>("[Operator1]: Finish"));
    final Object[] output = testHarness.getOutput().toArray();
    assertArrayEquals("Output was not correct.", expected.toArray(), output);
}
Also used : ArrayList(java.util.ArrayList) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) FromElementsFunction(org.apache.flink.streaming.api.functions.source.FromElementsFunction) Test(org.junit.Test)

Example 2 with FromElementsFunction

use of org.apache.flink.streaming.api.functions.source.FromElementsFunction in project flink by apache.

the class FromElementsFunctionTest method testSetOutputTypeWithSameSerializer.

@Test
public void testSetOutputTypeWithSameSerializer() throws Exception {
    FromElementsFunction<String> source = new FromElementsFunction<>(BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new ExecutionConfig()), STRING_LIST_DATA);
    TypeSerializer<String> existingSerializer = source.getSerializer();
    source.setOutputType(BasicTypeInfo.STRING_TYPE_INFO, new ExecutionConfig());
    TypeSerializer<String> newSerializer = source.getSerializer();
    assertEquals(existingSerializer, newSerializer);
    List<String> result = runSource(source);
    assertEquals(STRING_LIST_DATA, result);
}
Also used : FromElementsFunction(org.apache.flink.streaming.api.functions.source.FromElementsFunction) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Test(org.junit.Test)

Example 3 with FromElementsFunction

use of org.apache.flink.streaming.api.functions.source.FromElementsFunction in project flink by apache.

the class StreamExecutionEnvironmentTest method testSources.

@Test
public void testSources() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    SourceFunction<Integer> srcFun = new SourceFunction<Integer>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void run(SourceContext<Integer> ctx) throws Exception {
        }

        @Override
        public void cancel() {
        }
    };
    DataStreamSource<Integer> src1 = env.addSource(srcFun);
    src1.addSink(new DiscardingSink<Integer>());
    assertEquals(srcFun, getFunctionFromDataSource(src1));
    List<Long> list = Arrays.asList(0L, 1L, 2L);
    DataStreamSource<Long> src2 = env.generateSequence(0, 2);
    assertTrue(getFunctionFromDataSource(src2) instanceof StatefulSequenceSource);
    DataStreamSource<Long> src3 = env.fromElements(0L, 1L, 2L);
    assertTrue(getFunctionFromDataSource(src3) instanceof FromElementsFunction);
    DataStreamSource<Long> src4 = env.fromCollection(list);
    assertTrue(getFunctionFromDataSource(src4) instanceof FromElementsFunction);
}
Also used : SourceFunction(org.apache.flink.streaming.api.functions.source.SourceFunction) FromElementsFunction(org.apache.flink.streaming.api.functions.source.FromElementsFunction) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StatefulSequenceSource(org.apache.flink.streaming.api.functions.source.StatefulSequenceSource) Test(org.junit.Test)

Example 4 with FromElementsFunction

use of org.apache.flink.streaming.api.functions.source.FromElementsFunction in project flink by apache.

the class FromElementsFunctionTest method testNonJavaSerializableTypeWithSetOutputType.

@Test
public void testNonJavaSerializableTypeWithSetOutputType() throws Exception {
    MyPojo[] data = { new MyPojo(1, 2), new MyPojo(3, 4), new MyPojo(5, 6) };
    FromElementsFunction<MyPojo> source = new FromElementsFunction<>(data);
    source.setOutputType(TypeExtractor.getForClass(MyPojo.class), new ExecutionConfig());
    List<MyPojo> result = runSource(source);
    assertEquals(Arrays.asList(data), result);
}
Also used : FromElementsFunction(org.apache.flink.streaming.api.functions.source.FromElementsFunction) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Test(org.junit.Test)

Example 5 with FromElementsFunction

use of org.apache.flink.streaming.api.functions.source.FromElementsFunction in project flink by apache.

the class FromElementsFunctionTest method testSetOutputTypeWithIncompatibleType.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testSetOutputTypeWithIncompatibleType() throws Exception {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("not all subclasses of java.lang.Integer");
    FromElementsFunction<String> source = new FromElementsFunction<>(STRING_LIST_DATA);
    source.setOutputType((TypeInformation) BasicTypeInfo.INT_TYPE_INFO, new ExecutionConfig());
}
Also used : FromElementsFunction(org.apache.flink.streaming.api.functions.source.FromElementsFunction) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Test(org.junit.Test)

Aggregations

FromElementsFunction (org.apache.flink.streaming.api.functions.source.FromElementsFunction)10 Test (org.junit.Test)10 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)6 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)3 ArrayList (java.util.ArrayList)2 SourceFunction (org.apache.flink.streaming.api.functions.source.SourceFunction)2 IOException (java.io.IOException)1 GenericTypeInfo (org.apache.flink.api.java.typeutils.GenericTypeInfo)1 ValueTypeInfo (org.apache.flink.api.java.typeutils.ValueTypeInfo)1 OperatorSubtaskState (org.apache.flink.runtime.checkpoint.OperatorSubtaskState)1 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)1 StatefulSequenceSource (org.apache.flink.streaming.api.functions.source.StatefulSequenceSource)1 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)1 StreamSource (org.apache.flink.streaming.api.operators.StreamSource)1 AbstractStreamOperatorTestHarness (org.apache.flink.streaming.util.AbstractStreamOperatorTestHarness)1 ExpectedException (org.junit.rules.ExpectedException)1