org.apache.flink.test.streaming.runtime.util.TestListResultSink Java Examples

The following examples show how to use org.apache.flink.test.streaming.runtime.util.TestListResultSink. 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: DirectedOutputITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void outputSelectorTest() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.setParallelism(3);

	TestListResultSink<Long> evenSink = new TestListResultSink<Long>();
	TestListResultSink<Long> oddAndTenSink = new TestListResultSink<Long>();
	TestListResultSink<Long> evenAndOddSink = new TestListResultSink<Long>();
	TestListResultSink<Long> allSink = new TestListResultSink<Long>();

	SplitStream<Long> source = env.generateSequence(1, 11).split(new MyOutputSelector());
	source.select(EVEN).addSink(evenSink);
	source.select(ODD, TEN).addSink(oddAndTenSink);
	source.select(EVEN, ODD).addSink(evenAndOddSink);
	source.addSink(allSink);

	env.execute();
	assertEquals(Arrays.asList(2L, 4L, 6L, 8L, 10L), evenSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 3L, 5L, 7L, 9L, 10L, 11L), oddAndTenSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L),
			evenAndOddSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L),
			allSink.getSortedResult());
}
 
Example #2
Source File: DirectedOutputITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void outputSelectorTest() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.setParallelism(3);

	TestListResultSink<Long> evenSink = new TestListResultSink<Long>();
	TestListResultSink<Long> oddAndTenSink = new TestListResultSink<Long>();
	TestListResultSink<Long> evenAndOddSink = new TestListResultSink<Long>();
	TestListResultSink<Long> allSink = new TestListResultSink<Long>();

	SplitStream<Long> source = env.generateSequence(1, 11).split(new MyOutputSelector());
	source.select(EVEN).addSink(evenSink);
	source.select(ODD, TEN).addSink(oddAndTenSink);
	source.select(EVEN, ODD).addSink(evenAndOddSink);
	source.addSink(allSink);

	env.execute();
	assertEquals(Arrays.asList(2L, 4L, 6L, 8L, 10L), evenSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 3L, 5L, 7L, 9L, 10L, 11L), oddAndTenSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L),
			evenAndOddSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L),
			allSink.getSortedResult());
}
 
Example #3
Source File: DirectedOutputITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void outputSelectorTest() throws Exception {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.setParallelism(3);

	TestListResultSink<Long> evenSink = new TestListResultSink<Long>();
	TestListResultSink<Long> oddAndTenSink = new TestListResultSink<Long>();
	TestListResultSink<Long> evenAndOddSink = new TestListResultSink<Long>();
	TestListResultSink<Long> allSink = new TestListResultSink<Long>();

	SplitStream<Long> source = env.generateSequence(1, 11).split(new MyOutputSelector());
	source.select(EVEN).addSink(evenSink);
	source.select(ODD, TEN).addSink(oddAndTenSink);
	source.select(EVEN, ODD).addSink(evenAndOddSink);
	source.addSink(allSink);

	env.execute();
	assertEquals(Arrays.asList(2L, 4L, 6L, 8L, 10L), evenSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 3L, 5L, 7L, 9L, 10L, 11L), oddAndTenSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L),
			evenAndOddSink.getSortedResult());
	assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L),
			allSink.getSortedResult());
}
 
Example #4
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Test ProcessFunction side output.
 */
@Test
public void testProcessFunctionSideOutput() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink);
	passThroughtStream.addSink(resultSink);
	see.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #5
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Test ProcessFunction side outputs with wrong {@code OutputTag}.
 */
@Test
public void testProcessFunctionSideOutputWithWrongTag() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("side"){};
	final OutputTag<String> sideOutputTag2 = new OutputTag<String>("other-side"){};

	TestListResultSink<String> sideOutputResultSink = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag2, "sideout-" + String.valueOf(value));
				}
			}).getSideOutput(sideOutputTag1).addSink(sideOutputResultSink);

	see.execute();

	assertEquals(Arrays.asList(), sideOutputResultSink.getSortedResult());
}
 
Example #6
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Test ProcessFunction side output.
 */
@Test
public void testProcessFunctionSideOutput() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink);
	passThroughtStream.addSink(resultSink);
	see.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #7
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputNameClash() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("side"){};
	final OutputTag<Integer> sideOutputTag2 = new OutputTag<Integer>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<Integer> sideOutputResultSink2 = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag1, "sideout-" + String.valueOf(value));
					ctx.output(sideOutputTag2, 13);
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag1).addSink(sideOutputResultSink1);

	expectedException.expect(UnsupportedOperationException.class);
	passThroughtStream.getSideOutput(sideOutputTag2).addSink(sideOutputResultSink2);
}
 
Example #8
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentSideOutputTypes() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("string"){};
	final OutputTag<Integer> sideOutputTag2 = new OutputTag<Integer>("int"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<Integer> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.getConfig().enableObjectReuse();
	env.setParallelism(3);

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag1, "sideout-" + String.valueOf(value));
					ctx.output(sideOutputTag2, 13);
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag1).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag2).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList(13, 13, 13, 13, 13), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #9
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputWithMultipleConsumersWithObjectReuse() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<String> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.getConfig().enableObjectReuse();
	env.setParallelism(3);

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #10
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputWithMultipleConsumers() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<String> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

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

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #11
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 #12
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 #13
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Test ProcessFunction side outputs with wrong {@code OutputTag}.
 */
@Test
public void testProcessFunctionSideOutputWithWrongTag() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("side"){};
	final OutputTag<String> sideOutputTag2 = new OutputTag<String>("other-side"){};

	TestListResultSink<String> sideOutputResultSink = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag2, "sideout-" + String.valueOf(value));
				}
			}).getSideOutput(sideOutputTag1).addSink(sideOutputResultSink);

	see.execute();

	assertEquals(Arrays.asList(), sideOutputResultSink.getSortedResult());
}
 
Example #14
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputNameClash() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("side"){};
	final OutputTag<Integer> sideOutputTag2 = new OutputTag<Integer>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<Integer> sideOutputResultSink2 = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag1, "sideout-" + String.valueOf(value));
					ctx.output(sideOutputTag2, 13);
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag1).addSink(sideOutputResultSink1);

	expectedException.expect(UnsupportedOperationException.class);
	passThroughtStream.getSideOutput(sideOutputTag2).addSink(sideOutputResultSink2);
}
 
Example #15
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputWithMultipleConsumersWithObjectReuse() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<String> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.getConfig().enableObjectReuse();
	env.setParallelism(3);

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #16
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentSideOutputTypes() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("string"){};
	final OutputTag<Integer> sideOutputTag2 = new OutputTag<Integer>("int"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<Integer> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.getConfig().enableObjectReuse();
	env.setParallelism(3);

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag1, "sideout-" + String.valueOf(value));
					ctx.output(sideOutputTag2, 13);
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag1).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag2).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList(13, 13, 13, 13, 13), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #17
Source File: SideOutputITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputWithMultipleConsumers() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<String> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

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

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #18
Source File: SideOutputITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputWithMultipleConsumers() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<String> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

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

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #19
Source File: SideOutputITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputWithMultipleConsumersWithObjectReuse() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<String> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.getConfig().enableObjectReuse();
	env.setParallelism(3);

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #20
Source File: SideOutputITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentSideOutputTypes() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("string"){};
	final OutputTag<Integer> sideOutputTag2 = new OutputTag<Integer>("int"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<Integer> sideOutputResultSink2 = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	env.getConfig().enableObjectReuse();
	env.setParallelism(3);

	DataStream<Integer> dataStream = env.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag1, "sideout-" + String.valueOf(value));
					ctx.output(sideOutputTag2, 13);
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag1).addSink(sideOutputResultSink1);
	passThroughtStream.getSideOutput(sideOutputTag2).addSink(sideOutputResultSink2);
	passThroughtStream.addSink(resultSink);
	env.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink1.getSortedResult());
	assertEquals(Arrays.asList(13, 13, 13, 13, 13), sideOutputResultSink2.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #21
Source File: SideOutputITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideOutputNameClash() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("side"){};
	final OutputTag<Integer> sideOutputTag2 = new OutputTag<Integer>("side"){};

	TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
	TestListResultSink<Integer> sideOutputResultSink2 = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag1, "sideout-" + String.valueOf(value));
					ctx.output(sideOutputTag2, 13);
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag1).addSink(sideOutputResultSink1);

	expectedException.expect(UnsupportedOperationException.class);
	passThroughtStream.getSideOutput(sideOutputTag2).addSink(sideOutputResultSink2);
}
 
Example #22
Source File: SideOutputITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Test ProcessFunction side output.
 */
@Test
public void testProcessFunctionSideOutput() throws Exception {
	final OutputTag<String> sideOutputTag = new OutputTag<String>("side"){};

	TestListResultSink<String> sideOutputResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	SingleOutputStreamOperator<Integer> passThroughtStream = dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag, "sideout-" + String.valueOf(value));
				}
			});

	passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink);
	passThroughtStream.addSink(resultSink);
	see.execute();

	assertEquals(Arrays.asList("sideout-1", "sideout-2", "sideout-3", "sideout-4", "sideout-5"), sideOutputResultSink.getSortedResult());
	assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
 
Example #23
Source File: SideOutputITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Test ProcessFunction side outputs with wrong {@code OutputTag}.
 */
@Test
public void testProcessFunctionSideOutputWithWrongTag() throws Exception {
	final OutputTag<String> sideOutputTag1 = new OutputTag<String>("side"){};
	final OutputTag<String> sideOutputTag2 = new OutputTag<String>("other-side"){};

	TestListResultSink<String> sideOutputResultSink = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	dataStream
			.process(new ProcessFunction<Integer, Integer>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void processElement(
						Integer value, Context ctx, Collector<Integer> out) throws Exception {
					out.collect(value);
					ctx.output(sideOutputTag2, "sideout-" + String.valueOf(value));
				}
			}).getSideOutput(sideOutputTag1).addSink(sideOutputResultSink);

	see.execute();

	assertEquals(Arrays.asList(), sideOutputResultSink.getSortedResult());
}
 
Example #24
Source File: SideOutputITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testUnionOfTwoSideOutputs() throws Exception {
	TestListResultSink<Integer> evensResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> oddsResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> oddsUEvensResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> evensUOddsResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> oddsUOddsResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> evensUEvensResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> oddsUEvensExternalResultSink = new TestListResultSink<>();
	TestListResultSink<Integer> resultSink = new TestListResultSink<>();

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

	DataStream<Integer> input = env.fromElements(1, 2, 3, 4);

	OutputTag<Integer> oddTag = new OutputTag<Integer>("odds"){};
	OutputTag<Integer> evenTag = new OutputTag<Integer>("even"){};

	SingleOutputStreamOperator<Integer> passThroughStream =
		input.process(new ProcessFunction<Integer, Integer>() {
			@Override
			public void processElement(Integer value, Context ctx, Collector<Integer> out) throws Exception {
				if (value % 2 != 0) {
					ctx.output(oddTag, value);
				}
				else {
					ctx.output(evenTag, value);
				}
				out.collect(value);
			}
		});

	DataStream<Integer> evens = passThroughStream.getSideOutput(evenTag);
	DataStream<Integer> odds = passThroughStream.getSideOutput(oddTag);

	evens.addSink(evensResultSink);
	odds.addSink(oddsResultSink);
	passThroughStream.addSink(resultSink);

	odds.union(evens).addSink(oddsUEvensResultSink);
	evens.union(odds).addSink(evensUOddsResultSink);

	odds.union(odds).addSink(oddsUOddsResultSink);
	evens.union(evens).addSink(evensUEvensResultSink);

	odds.union(env.fromElements(2, 4)).addSink(oddsUEvensExternalResultSink);

	env.execute();

	assertEquals(
		Arrays.asList(1, 3),
		oddsResultSink.getSortedResult());

	assertEquals(
		Arrays.asList(2, 4),
		evensResultSink.getSortedResult());

	assertEquals(
		Arrays.asList(1, 2, 3, 4),
		resultSink.getSortedResult());

	assertEquals(
		Arrays.asList(1, 2, 3, 4),
		oddsUEvensResultSink.getSortedResult());

	assertEquals(
		Arrays.asList(1, 2, 3, 4),
		evensUOddsResultSink.getSortedResult());

	assertEquals(
		Arrays.asList(1, 1, 3, 3),
		oddsUOddsResultSink.getSortedResult());

	assertEquals(
		Arrays.asList(2, 2, 4, 4),
		evensUEvensResultSink.getSortedResult());

	assertEquals(
		Arrays.asList(1, 2, 3, 4),
		oddsUEvensExternalResultSink.getSortedResult());
}
 
Example #25
Source File: SideOutputITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyedWindowLateArrivingEvents() throws Exception {
	TestListResultSink<String> resultSink = new TestListResultSink<>();
	TestListResultSink<Integer> lateResultSink = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);
	see.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	OutputTag<Integer> lateDataTag = new OutputTag<Integer>("late"){};

	SingleOutputStreamOperator<String> windowOperator = dataStream
			.assignTimestampsAndWatermarks(new TestWatermarkAssigner())
			.keyBy(new TestKeySelector())
			.timeWindow(Time.milliseconds(1), Time.milliseconds(1))
			.allowedLateness(Time.milliseconds(2))
			.sideOutputLateData(lateDataTag)
			.apply(new WindowFunction<Integer, String, Integer, TimeWindow>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void apply(Integer key, TimeWindow window, Iterable<Integer> input, Collector<String> out) throws Exception {
					for (Integer val : input) {
						out.collect(String.valueOf(key) + "-" + String.valueOf(val));
					}
				}
			});

	windowOperator
			.addSink(resultSink);

	windowOperator
			.getSideOutput(lateDataTag)
			.addSink(lateResultSink);

	see.execute();
	assertEquals(Arrays.asList("1-1", "2-2", "4-4", "5-5"), resultSink.getSortedResult());
	assertEquals(Collections.singletonList(3), lateResultSink.getSortedResult());
}
 
Example #26
Source File: StreamTaskSelectiveReadingITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testSequentialReading() throws Exception {

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

	DataStream<String> source0 = env.addSource(
		new TestStringSource("Source0",
			new String[] {
				"Hello-1", "Hello-2", "Hello-3", "Hello-4", "Hello-5", "Hello-6"
		}));
	DataStream<Integer> source1 = env.addSource(
		new TestIntegerSource("Source1",
			new Integer[] {
				1, 2, 3
			}))
		.setParallelism(2);
	TestListResultSink<String> resultSink = new TestListResultSink<>();

	TestSequentialReadingStreamOperator twoInputStreamOperator = new TestSequentialReadingStreamOperator("Operator0");
	twoInputStreamOperator.setChainingStrategy(ChainingStrategy.NEVER);

	source0.connect(source1)
		.transform(
			"Custom Operator",
			BasicTypeInfo.STRING_TYPE_INFO,
			twoInputStreamOperator
		)
		.addSink(resultSink);

	env.execute("Selective reading test");

	List<String> result = resultSink.getResult();

	List<String> expected1 = Arrays.asList(
		"[Operator0-1]: [Source0-0]: Hello-1",
		"[Operator0-1]: [Source0-0]: Hello-2",
		"[Operator0-1]: [Source0-0]: Hello-3",
		"[Operator0-1]: [Source0-0]: Hello-4",
		"[Operator0-1]: [Source0-0]: Hello-5",
		"[Operator0-1]: [Source0-0]: Hello-6"
	);

	List<String> expected2 = Arrays.asList(
		"[Operator0-2]: 1",
		"[Operator0-2]: 2",
		"[Operator0-2]: 3",
		"[Operator0-2]: 2",
		"[Operator0-2]: 4",
		"[Operator0-2]: 6"
	);
	Collections.sort(expected2);

	assertEquals(expected1.size() + expected2.size(), result.size());
	assertEquals(expected1, result.subList(0, expected1.size()));

	List<String> result2 = result.subList(expected1.size(), expected1.size() + expected2.size());
	Collections.sort(result2);
	assertEquals(expected2, result2);
}
 
Example #27
Source File: SideOutputITCase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyedWindowLateArrivingEvents() throws Exception {
	TestListResultSink<String> resultSink = new TestListResultSink<>();
	TestListResultSink<Integer> lateResultSink = new TestListResultSink<>();

	StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
	see.setParallelism(3);
	see.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

	DataStream<Integer> dataStream = see.fromCollection(elements);

	OutputTag<Integer> lateDataTag = new OutputTag<Integer>("late"){};

	SingleOutputStreamOperator<String> windowOperator = dataStream
			.assignTimestampsAndWatermarks(new TestWatermarkAssigner())
			.keyBy(new TestKeySelector())
			.timeWindow(Time.milliseconds(1), Time.milliseconds(1))
			.allowedLateness(Time.milliseconds(2))
			.sideOutputLateData(lateDataTag)
			.apply(new WindowFunction<Integer, String, Integer, TimeWindow>() {
				private static final long serialVersionUID = 1L;

				@Override
				public void apply(Integer key, TimeWindow window, Iterable<Integer> input, Collector<String> out) throws Exception {
					for (Integer val : input) {
						out.collect(String.valueOf(key) + "-" + String.valueOf(val));
					}
				}
			});

	windowOperator
			.addSink(resultSink);

	windowOperator
			.getSideOutput(lateDataTag)
			.addSink(lateResultSink);

	see.execute();
	assertEquals(Arrays.asList("1-1", "2-2", "4-4", "5-5"), resultSink.getSortedResult());
	assertEquals(Collections.singletonList(3), lateResultSink.getSortedResult());
}
 
Example #28
Source File: PartitionerITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void partitionerTest() {

	TestListResultSink<Tuple2<Integer, String>> hashPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> customPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> broadcastPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> forwardPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> rebalancePartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> globalPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();

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

	DataStream<Tuple1<String>> src = env.fromCollection(
		INPUT.stream().map(Tuple1::of).collect(Collectors.toList()));

	// partition by hash
	src
			.keyBy(0)
			.map(new SubtaskIndexAssigner())
			.addSink(hashPartitionResultSink);

	// partition custom
	DataStream<Tuple2<Integer, String>> partitionCustom = src
			.partitionCustom(new Partitioner<String>() {
				@Override
				public int partition(String key, int numPartitions) {
					if (key.equals("c")) {
						return 2;
					} else {
						return 0;
					}
				}
			}, 0)
			.map(new SubtaskIndexAssigner());

	partitionCustom.addSink(customPartitionResultSink);

	// partition broadcast
	src.broadcast().map(new SubtaskIndexAssigner()).addSink(broadcastPartitionResultSink);

	// partition rebalance
	src.rebalance().map(new SubtaskIndexAssigner()).addSink(rebalancePartitionResultSink);

	// partition forward
	src.map(new MapFunction<Tuple1<String>, Tuple1<String>>() {
		private static final long serialVersionUID = 1L;
		@Override
		public Tuple1<String> map(Tuple1<String> value) throws Exception {
			return value;
		}
	})
			.forward()
			.map(new SubtaskIndexAssigner())
			.addSink(forwardPartitionResultSink);

	// partition global
	src.global().map(new SubtaskIndexAssigner()).addSink(globalPartitionResultSink);

	try {
		env.execute();
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}

	List<Tuple2<Integer, String>> hashPartitionResult = hashPartitionResultSink.getResult();
	List<Tuple2<Integer, String>> customPartitionResult = customPartitionResultSink.getResult();
	List<Tuple2<Integer, String>> broadcastPartitionResult = broadcastPartitionResultSink.getResult();
	List<Tuple2<Integer, String>> forwardPartitionResult = forwardPartitionResultSink.getResult();
	List<Tuple2<Integer, String>> rebalancePartitionResult = rebalancePartitionResultSink.getResult();
	List<Tuple2<Integer, String>> globalPartitionResult = globalPartitionResultSink.getResult();

	verifyHashPartitioning(hashPartitionResult);
	verifyCustomPartitioning(customPartitionResult);
	verifyBroadcastPartitioning(broadcastPartitionResult);
	verifyRebalancePartitioning(forwardPartitionResult);
	verifyRebalancePartitioning(rebalancePartitionResult);
	verifyGlobalPartitioning(globalPartitionResult);
}
 
Example #29
Source File: StreamTaskSelectiveReadingITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testSequentialReading() throws Exception {

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

	DataStream<String> source0 = env.addSource(
		new TestStringSource("Source0",
			new String[] {
				"Hello-1", "Hello-2", "Hello-3", "Hello-4", "Hello-5", "Hello-6"
		}));
	DataStream<Integer> source1 = env.addSource(
		new TestIntegerSource("Source1",
			new Integer[] {
				1, 2, 3
			}))
		.setParallelism(2);
	TestListResultSink<String> resultSink = new TestListResultSink<>();

	TwoInputStreamOperator<String, Integer, String> twoInputStreamOperator = new SequentialReadingStreamOperator("Operator0");
	twoInputStreamOperator.setChainingStrategy(ChainingStrategy.NEVER);

	source0.connect(source1)
		.transform(
			"Custom Operator",
			BasicTypeInfo.STRING_TYPE_INFO,
			twoInputStreamOperator
		)
		.addSink(resultSink);

	env.execute("Selective reading test");

	List<String> result = resultSink.getResult();

	List<String> expected1 = Arrays.asList(
		"[Operator0-1]: [Source0-0]: Hello-1",
		"[Operator0-1]: [Source0-0]: Hello-2",
		"[Operator0-1]: [Source0-0]: Hello-3",
		"[Operator0-1]: [Source0-0]: Hello-4",
		"[Operator0-1]: [Source0-0]: Hello-5",
		"[Operator0-1]: [Source0-0]: Hello-6"
	);

	List<String> expected2 = Arrays.asList(
		"[Operator0-2]: 1",
		"[Operator0-2]: 2",
		"[Operator0-2]: 3",
		"[Operator0-2]: 2",
		"[Operator0-2]: 4",
		"[Operator0-2]: 6"
	);
	Collections.sort(expected2);

	assertEquals(expected1.size() + expected2.size(), result.size());
	assertEquals(expected1, result.subList(0, expected1.size()));

	List<String> result2 = result.subList(expected1.size(), expected1.size() + expected2.size());
	Collections.sort(result2);
	assertEquals(expected2, result2);
}
 
Example #30
Source File: PartitionerITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void partitionerTest() {

	TestListResultSink<Tuple2<Integer, String>> hashPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> customPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> broadcastPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> forwardPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> rebalancePartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();
	TestListResultSink<Tuple2<Integer, String>> globalPartitionResultSink =
			new TestListResultSink<Tuple2<Integer, String>>();

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

	DataStream<Tuple1<String>> src = env.fromCollection(
		INPUT.stream().map(Tuple1::of).collect(Collectors.toList()));

	// partition by hash
	src
			.keyBy(0)
			.map(new SubtaskIndexAssigner())
			.addSink(hashPartitionResultSink);

	// partition custom
	DataStream<Tuple2<Integer, String>> partitionCustom = src
			.partitionCustom(new Partitioner<String>() {
				@Override
				public int partition(String key, int numPartitions) {
					if (key.equals("c")) {
						return 2;
					} else {
						return 0;
					}
				}
			}, 0)
			.map(new SubtaskIndexAssigner());

	partitionCustom.addSink(customPartitionResultSink);

	// partition broadcast
	src.broadcast().map(new SubtaskIndexAssigner()).addSink(broadcastPartitionResultSink);

	// partition rebalance
	src.rebalance().map(new SubtaskIndexAssigner()).addSink(rebalancePartitionResultSink);

	// partition forward
	src.map(new MapFunction<Tuple1<String>, Tuple1<String>>() {
		private static final long serialVersionUID = 1L;
		@Override
		public Tuple1<String> map(Tuple1<String> value) throws Exception {
			return value;
		}
	})
			.forward()
			.map(new SubtaskIndexAssigner())
			.addSink(forwardPartitionResultSink);

	// partition global
	src.global().map(new SubtaskIndexAssigner()).addSink(globalPartitionResultSink);

	try {
		env.execute();
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}

	List<Tuple2<Integer, String>> hashPartitionResult = hashPartitionResultSink.getResult();
	List<Tuple2<Integer, String>> customPartitionResult = customPartitionResultSink.getResult();
	List<Tuple2<Integer, String>> broadcastPartitionResult = broadcastPartitionResultSink.getResult();
	List<Tuple2<Integer, String>> forwardPartitionResult = forwardPartitionResultSink.getResult();
	List<Tuple2<Integer, String>> rebalancePartitionResult = rebalancePartitionResultSink.getResult();
	List<Tuple2<Integer, String>> globalPartitionResult = globalPartitionResultSink.getResult();

	verifyHashPartitioning(hashPartitionResult);
	verifyCustomPartitioning(customPartitionResult);
	verifyBroadcastPartitioning(broadcastPartitionResult);
	verifyRebalancePartitioning(forwardPartitionResult);
	verifyRebalancePartitioning(rebalancePartitionResult);
	verifyGlobalPartitioning(globalPartitionResult);
}