Java Code Examples for org.apache.flink.streaming.api.environment.StreamExecutionEnvironment#addOperator()

The following examples show how to use org.apache.flink.streaming.api.environment.StreamExecutionEnvironment#addOperator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataStreamUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an iterator to iterate over the elements of the DataStream.
 * @return The iterator
 */
public static <OUT> Iterator<OUT> collect(DataStream<OUT> stream) {
	TypeSerializer<OUT> serializer = stream.getType().createSerializer(
		stream.getExecutionEnvironment().getConfig());
	String accumulatorName = "dataStreamCollect_" + UUID.randomUUID().toString();

	CollectSinkOperatorFactory<OUT> factory = new CollectSinkOperatorFactory<>(serializer, accumulatorName);
	CollectSinkOperator<OUT> operator = (CollectSinkOperator<OUT>) factory.getOperator();
	CollectResultIterator<OUT> iterator = new CollectResultIterator<>(
		operator.getOperatorIdFuture(), serializer, accumulatorName);
	CollectStreamSink<OUT> sink = new CollectStreamSink<>(stream, factory);
	sink.name("Data stream collect sink");

	StreamExecutionEnvironment env = stream.getExecutionEnvironment();
	env.addOperator(sink.getTransformation());

	try {
		JobClient jobClient = env.executeAsync("Data Stream Collect");
		iterator.setJobClient(jobClient);
	} catch (Exception e) {
		throw new RuntimeException("Failed to execute data stream", e);
	}

	return iterator;
}
 
Example 2
Source File: MultipleInputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.setParallelism(1);

	TestListResultSink<Long> resultSink = new TestListResultSink<>();

	DataStream<Integer> source1 = env.fromElements(1, 10);
	DataStream<Long> source2 = env.fromElements(2L, 11L);
	DataStream<String> source3 = env.fromElements("42", "44");

	MultipleInputTransformation<Long> transform = new MultipleInputTransformation<>(
		"My Operator",
		new SumAllInputOperatorFactory(),
		BasicTypeInfo.LONG_TYPE_INFO,
		1);

	env.addOperator(transform
		.addInput(source1.getTransformation())
		.addInput(source2.getTransformation())
		.addInput(source3.getTransformation()));

	new MultipleConnectedStreams(env)
		.transform(transform)
		.addSink(resultSink);

	env.execute();

	List<Long> result = resultSink.getResult();
	Collections.sort(result);
	long actualSum = result.get(result.size() - 1);
	assertEquals(1 + 10 + 2 + 11 + 42 + 44, actualSum);
}
 
Example 3
Source File: MultipleInputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeyedState() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.setParallelism(1);

	TestListResultSink<Long> resultSink = new TestListResultSink<>();

	DataStream<Long> source1 = env.fromElements(0L, 3L);
	DataStream<Long> source2 = env.fromElements(13L, 16L);
	DataStream<Long> source3 = env.fromElements(101L, 104L);

	KeyedMultipleInputTransformation<Long> transform = new KeyedMultipleInputTransformation<>(
		"My Operator",
		new KeyedSumMultipleInputOperatorFactory(),
		BasicTypeInfo.LONG_TYPE_INFO,
		1,
		BasicTypeInfo.LONG_TYPE_INFO);
	KeySelector<Long, Long> keySelector = (KeySelector<Long, Long>) value -> value % 3;

	env.addOperator(transform
		.addInput(source1.getTransformation(), keySelector)
		.addInput(source2.getTransformation(), keySelector)
		.addInput(source3.getTransformation(), keySelector));

	new MultipleConnectedStreams(env)
		.transform(transform)
		.addSink(resultSink);

	env.execute();

	List<Long> result = resultSink.getResult();
	Collections.sort(result);
	assertThat(result, contains(0L, 3L, 13L, 13L + 16L, 101L, 101L + 104L));
}
 
Example 4
Source File: StreamGraphGeneratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleInputTransformation() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStream<Integer> source1 = env.fromElements(1, 10);
	DataStream<Long> source2 = env.fromElements(2L, 11L);
	DataStream<String> source3 = env.fromElements("42", "44");

	MultipleInputTransformation<String> transform = new MultipleInputTransformation<String>(
		"My Operator",
		new MultipleInputOperatorFactory(),
		BasicTypeInfo.STRING_TYPE_INFO,
		3);

	env.addOperator(transform
		.addInput(source1.getTransformation())
		.addInput(source2.getTransformation())
		.addInput(source3.getTransformation()));

	StreamGraph streamGraph = env.getStreamGraph();
	assertEquals(4, streamGraph.getStreamNodes().size());

	assertEquals(1, streamGraph.getStreamEdges(source1.getId(), transform.getId()).size());
	assertEquals(1, streamGraph.getStreamEdges(source2.getId(), transform.getId()).size());
	assertEquals(1, streamGraph.getStreamEdges(source3.getId(), transform.getId()).size());
	assertEquals(1, streamGraph.getStreamEdges(source1.getId()).size());
	assertEquals(1, streamGraph.getStreamEdges(source2.getId()).size());
	assertEquals(1, streamGraph.getStreamEdges(source3.getId()).size());
	assertEquals(0, streamGraph.getStreamEdges(transform.getId()).size());
}