use of org.apache.flink.util.OutputTag in project flink by apache.
the class CEPITCase method testFlatSelectSerializationWithAnonymousClass.
@Test
public void testFlatSelectSerializationWithAnonymousClass() throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(envConfiguration);
DataStreamSource<Integer> elements = env.fromElements(1, 2, 3);
OutputTag<Integer> outputTag = new OutputTag<Integer>("AAA") {
};
CEP.pattern(elements, Pattern.begin("A")).inProcessingTime().flatSelect(outputTag, new PatternFlatTimeoutFunction<Integer, Integer>() {
@Override
public void timeout(Map<String, List<Integer>> pattern, long timeoutTimestamp, Collector<Integer> out) throws Exception {
}
}, new PatternFlatSelectFunction<Integer, Object>() {
@Override
public void flatSelect(Map<String, List<Integer>> pattern, Collector<Object> out) throws Exception {
}
});
env.execute();
}
use of org.apache.flink.util.OutputTag in project flink by apache.
the class CEPOperatorTest method testKeyedAdvancingTimeWithoutElements.
/**
* Tests that the internal time of a CEP operator advances only given watermarks. See FLINK-5033
*/
@Test
public void testKeyedAdvancingTimeWithoutElements() throws Exception {
final Event startEvent = new Event(42, "start", 1.0);
final long watermarkTimestamp1 = 5L;
final long watermarkTimestamp2 = 13L;
final Map<String, List<Event>> expectedSequence = new HashMap<>(2);
expectedSequence.put("start", Collections.<Event>singletonList(startEvent));
final OutputTag<Tuple2<Map<String, List<Event>>, Long>> timedOut = new OutputTag<Tuple2<Map<String, List<Event>>, Long>>("timedOut") {
};
final KeyedOneInputStreamOperatorTestHarness<Integer, Event, Map<String, List<Event>>> harness = new KeyedOneInputStreamOperatorTestHarness<>(new CepOperator<>(Event.createTypeSerializer(), false, new NFAFactory(true), null, null, new TimedOutProcessFunction(timedOut), null), new KeySelector<Event, Integer>() {
private static final long serialVersionUID = 7219185117566268366L;
@Override
public Integer getKey(Event value) throws Exception {
return value.getId();
}
}, BasicTypeInfo.INT_TYPE_INFO);
try {
String rocksDbPath = tempFolder.newFolder().getAbsolutePath();
RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend(new MemoryStateBackend());
rocksDBStateBackend.setDbStoragePath(rocksDbPath);
harness.setStateBackend(rocksDBStateBackend);
harness.setup(new KryoSerializer<>((Class<Map<String, List<Event>>>) (Object) Map.class, new ExecutionConfig()));
harness.open();
harness.processElement(new StreamRecord<>(startEvent, 3L));
harness.processWatermark(new Watermark(watermarkTimestamp1));
harness.processWatermark(new Watermark(watermarkTimestamp2));
Queue<Object> result = harness.getOutput();
Queue<StreamRecord<Tuple2<Map<String, List<Event>>, Long>>> sideOutput = harness.getSideOutput(timedOut);
assertEquals(2L, result.size());
assertEquals(1L, sideOutput.size());
Object watermark1 = result.poll();
assertTrue(watermark1 instanceof Watermark);
assertEquals(watermarkTimestamp1, ((Watermark) watermark1).getTimestamp());
Tuple2<Map<String, List<Event>>, Long> leftResult = sideOutput.poll().getValue();
assertEquals(watermarkTimestamp2, (long) leftResult.f1);
assertEquals(expectedSequence, leftResult.f0);
Object watermark2 = result.poll();
assertTrue(watermark2 instanceof Watermark);
assertEquals(watermarkTimestamp2, ((Watermark) watermark2).getTimestamp());
} finally {
harness.close();
}
}
use of org.apache.flink.util.OutputTag in project flink by apache.
the class IterateITCase method testmultipleHeadsTailsWithTailPartitioning.
@Test
public void testmultipleHeadsTailsWithTailPartitioning() {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<Integer> source1 = env.fromElements(1, 2, 3, 4, 5).shuffle().map(noOpIntMap);
DataStream<Integer> source2 = env.fromElements(1, 2, 3, 4, 5).map(noOpIntMap);
IterativeStream<Integer> iter1 = source1.union(source2).iterate();
DataStream<Integer> head1 = iter1.map(noOpIntMap).name("map1");
DataStream<Integer> head2 = iter1.map(noOpIntMap).setParallelism(parallelism / 2).name("shuffle").rebalance();
DataStreamSink<Integer> head3 = iter1.map(noOpIntMap).setParallelism(parallelism / 2).addSink(new ReceiveCheckNoOpSink<Integer>());
DataStreamSink<Integer> head4 = iter1.map(noOpIntMap).addSink(new ReceiveCheckNoOpSink<Integer>());
OutputTag<Integer> even = new OutputTag<Integer>("even") {
};
OutputTag<Integer> odd = new OutputTag<Integer>("odd") {
};
SingleOutputStreamOperator<Object> source3 = env.fromElements(1, 2, 3, 4, 5).map(noOpIntMap).name("split").process(new ProcessFunction<Integer, Object>() {
@Override
public void processElement(Integer value, Context ctx, Collector<Object> out) throws Exception {
if (value % 2 == 0) {
ctx.output(even, value);
} else {
ctx.output(odd, value);
}
}
});
iter1.closeWith(source3.getSideOutput(even).union(head1.map(noOpIntMap).name("bc").broadcast(), head2.map(noOpIntMap).shuffle()));
StreamGraph graph = env.getStreamGraph();
JobGraph jg = graph.getJobGraph();
assertEquals(1, graph.getIterationSourceSinkPairs().size());
Tuple2<StreamNode, StreamNode> sourceSinkPair = graph.getIterationSourceSinkPairs().iterator().next();
StreamNode itSource = sourceSinkPair.f0;
StreamNode itSink = sourceSinkPair.f1;
assertEquals(4, itSource.getOutEdges().size());
assertEquals(3, itSink.getInEdges().size());
assertEquals(itSource.getParallelism(), itSink.getParallelism());
for (StreamEdge edge : itSource.getOutEdges()) {
if (graph.getTargetVertex(edge).getOperatorName().equals("map1")) {
assertTrue(edge.getPartitioner() instanceof ForwardPartitioner);
assertEquals(4, graph.getTargetVertex(edge).getParallelism());
} else if (graph.getTargetVertex(edge).getOperatorName().equals("shuffle")) {
assertTrue(edge.getPartitioner() instanceof RebalancePartitioner);
assertEquals(2, graph.getTargetVertex(edge).getParallelism());
}
}
for (StreamEdge edge : itSink.getInEdges()) {
String tailName = graph.getSourceVertex(edge).getOperatorName();
if (tailName.equals("split")) {
assertTrue(edge.getPartitioner() instanceof ForwardPartitioner);
} else if (tailName.equals("bc")) {
assertTrue(edge.getPartitioner() instanceof BroadcastPartitioner);
} else if (tailName.equals("shuffle")) {
assertTrue(edge.getPartitioner() instanceof ShufflePartitioner);
}
}
// Test co-location
JobVertex itSource1 = null;
JobVertex itSink1 = null;
for (JobVertex vertex : jg.getVertices()) {
if (vertex.getName().contains("IterationSource")) {
itSource1 = vertex;
} else if (vertex.getName().contains("IterationSink")) {
itSink1 = vertex;
}
}
assertTrue(itSource1.getCoLocationGroup() != null);
assertTrue(itSink1.getCoLocationGroup() != null);
assertEquals(itSource1.getCoLocationGroup(), itSink1.getCoLocationGroup());
}
use of org.apache.flink.util.OutputTag in project flink by apache.
the class SideOutputITCase method testKeyedCoProcessFunctionSideOutputWithMultipleConsumers.
/**
* Test keyed KeyedCoProcessFunction side output with multiple consumers.
*/
@Test
public void testKeyedCoProcessFunctionSideOutputWithMultipleConsumers() throws Exception {
final OutputTag<String> sideOutputTag1 = new OutputTag<String>("side1") {
};
final OutputTag<String> sideOutputTag2 = new OutputTag<String>("side2") {
};
TestListResultSink<String> sideOutputResultSink1 = new TestListResultSink<>();
TestListResultSink<String> sideOutputResultSink2 = new TestListResultSink<>();
TestListResultSink<Integer> resultSink = new TestListResultSink<>();
StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
see.setParallelism(3);
DataStream<Integer> ds1 = see.fromCollection(elements);
DataStream<Integer> ds2 = see.fromCollection(elements);
SingleOutputStreamOperator<Integer> passThroughtStream = ds1.keyBy(i -> i).connect(ds2.keyBy(i -> i)).process(new KeyedCoProcessFunction<Integer, Integer, Integer, Integer>() {
@Override
public void processElement1(Integer value, Context ctx, Collector<Integer> out) throws Exception {
if (value < 4) {
out.collect(value);
ctx.output(sideOutputTag1, "sideout1-" + ctx.getCurrentKey() + "-" + String.valueOf(value));
}
}
@Override
public void processElement2(Integer value, Context ctx, Collector<Integer> out) throws Exception {
if (value >= 4) {
out.collect(value);
ctx.output(sideOutputTag2, "sideout2-" + ctx.getCurrentKey() + "-" + String.valueOf(value));
}
}
});
passThroughtStream.getSideOutput(sideOutputTag1).addSink(sideOutputResultSink1);
passThroughtStream.getSideOutput(sideOutputTag2).addSink(sideOutputResultSink2);
passThroughtStream.addSink(resultSink);
see.execute();
assertEquals(Arrays.asList("sideout1-1-1", "sideout1-2-2", "sideout1-3-3"), sideOutputResultSink1.getSortedResult());
assertEquals(Arrays.asList("sideout2-4-4", "sideout2-5-5"), sideOutputResultSink2.getSortedResult());
assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
use of org.apache.flink.util.OutputTag in project flink by apache.
the class SideOutputITCase method testLegacyKeyedCoProcessFunctionSideOutput.
/**
* Test keyed CoProcessFunction side output.
*/
@Test
public void testLegacyKeyedCoProcessFunctionSideOutput() 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> ds1 = see.fromCollection(elements);
DataStream<Integer> ds2 = see.fromCollection(elements);
SingleOutputStreamOperator<Integer> passThroughtStream = ds1.keyBy(i -> i).connect(ds2.keyBy(i -> i)).process(new CoProcessFunction<Integer, Integer, Integer>() {
@Override
public void processElement1(Integer value, Context ctx, Collector<Integer> out) throws Exception {
if (value < 3) {
out.collect(value);
ctx.output(sideOutputTag, "sideout1-" + String.valueOf(value));
}
}
@Override
public void processElement2(Integer value, Context ctx, Collector<Integer> out) throws Exception {
if (value >= 3) {
out.collect(value);
ctx.output(sideOutputTag, "sideout2-" + String.valueOf(value));
}
}
});
passThroughtStream.getSideOutput(sideOutputTag).addSink(sideOutputResultSink);
passThroughtStream.addSink(resultSink);
see.execute();
assertEquals(Arrays.asList("sideout1-1", "sideout1-2", "sideout2-3", "sideout2-4", "sideout2-5"), sideOutputResultSink.getSortedResult());
assertEquals(Arrays.asList(1, 2, 3, 4, 5), resultSink.getSortedResult());
}
Aggregations