Search in sources :

Example 6 with AssignerWithPunctuatedWatermarks

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

the class CEPITCase method testTimeoutHandling.

@Test
public void testTimeoutHandling() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(envConfiguration);
    env.setParallelism(1);
    // (Event, timestamp)
    DataStream<Event> input = env.fromElements(Tuple2.of(new Event(1, "start", 1.0), 1L), Tuple2.of(new Event(1, "middle", 2.0), 5L), Tuple2.of(new Event(1, "start", 2.0), 4L), Tuple2.of(new Event(1, "end", 2.0), 6L)).assignTimestampsAndWatermarks(new AssignerWithPunctuatedWatermarks<Tuple2<Event, Long>>() {

        @Override
        public long extractTimestamp(Tuple2<Event, Long> element, long currentTimestamp) {
            return element.f1;
        }

        @Override
        public Watermark checkAndGetNextWatermark(Tuple2<Event, Long> lastElement, long extractedTimestamp) {
            return new Watermark(lastElement.f1 - 5);
        }
    }).map(new MapFunction<Tuple2<Event, Long>, Event>() {

        @Override
        public Event map(Tuple2<Event, Long> value) throws Exception {
            return value.f0;
        }
    });
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("start");
        }
    }).followedByAny("middle").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("middle");
        }
    }).followedByAny("end").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("end");
        }
    }).within(Time.milliseconds(3));
    DataStream<Either<String, String>> result = CEP.pattern(input, pattern).select(new PatternTimeoutFunction<Event, String>() {

        @Override
        public String timeout(Map<String, List<Event>> pattern, long timeoutTimestamp) throws Exception {
            return pattern.get("start").get(0).getPrice() + "";
        }
    }, new PatternSelectFunction<Event, String>() {

        @Override
        public String select(Map<String, List<Event>> pattern) {
            StringBuilder builder = new StringBuilder();
            builder.append(pattern.get("start").get(0).getPrice()).append(",").append(pattern.get("middle").get(0).getPrice()).append(",").append(pattern.get("end").get(0).getPrice());
            return builder.toString();
        }
    });
    List<Either<String, String>> resultList = new ArrayList<>();
    DataStreamUtils.collect(result).forEachRemaining(resultList::add);
    resultList.sort(Comparator.comparing(either -> either.toString()));
    List<Either<String, String>> expected = Arrays.asList(Either.Left.of("1.0"), Either.Left.of("2.0"), Either.Left.of("2.0"), Either.Right.of("2.0,2.0,2.0"));
    assertEquals(expected, resultList);
}
Also used : Arrays(java.util.Arrays) Tuple2(org.apache.flink.api.java.tuple.Tuple2) RichIterativeCondition(org.apache.flink.cep.pattern.conditions.RichIterativeCondition) Either(org.apache.flink.types.Either) RunWith(org.junit.runner.RunWith) Watermark(org.apache.flink.streaming.api.watermark.Watermark) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) DataStreamSource(org.apache.flink.streaming.api.datastream.DataStreamSource) MapFunction(org.apache.flink.api.common.functions.MapFunction) ArrayList(java.util.ArrayList) NFA(org.apache.flink.cep.nfa.NFA) Collector(org.apache.flink.util.Collector) Duration(java.time.Duration) Map(java.util.Map) LongSerializer(org.apache.flink.api.common.typeutils.base.LongSerializer) Pattern(org.apache.flink.cep.pattern.Pattern) AssignerWithPunctuatedWatermarks(org.apache.flink.streaming.api.functions.AssignerWithPunctuatedWatermarks) Parameterized(org.junit.runners.Parameterized) AbstractTestBase(org.apache.flink.test.util.AbstractTestBase) Types(org.apache.flink.api.common.typeinfo.Types) Time(org.apache.flink.streaming.api.windowing.time.Time) KeySelector(org.apache.flink.api.java.functions.KeySelector) Collection(java.util.Collection) Configuration(org.apache.flink.configuration.Configuration) DataStreamUtils(org.apache.flink.streaming.api.datastream.DataStreamUtils) OutputTag(org.apache.flink.util.OutputTag) Test(org.junit.Test) DataStream(org.apache.flink.streaming.api.datastream.DataStream) List(java.util.List) AfterMatchSkipStrategy(org.apache.flink.cep.nfa.aftermatch.AfterMatchSkipStrategy) CEPCacheOptions(org.apache.flink.cep.configuration.CEPCacheOptions) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) AssignerWithPunctuatedWatermarks(org.apache.flink.streaming.api.functions.AssignerWithPunctuatedWatermarks) Either(org.apache.flink.types.Either) ArrayList(java.util.ArrayList) List(java.util.List) Tuple2(org.apache.flink.api.java.tuple.Tuple2) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Aggregations

AssignerWithPunctuatedWatermarks (org.apache.flink.streaming.api.functions.AssignerWithPunctuatedWatermarks)6 Test (org.junit.Test)6 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)4 Watermark (org.apache.flink.streaming.api.watermark.Watermark)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)3 HashMap (java.util.HashMap)2 TestProcessingTimeService (org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService)2 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Map (java.util.Map)1 MapFunction (org.apache.flink.api.common.functions.MapFunction)1 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)1 Types (org.apache.flink.api.common.typeinfo.Types)1 LongSerializer (org.apache.flink.api.common.typeutils.base.LongSerializer)1 KeySelector (org.apache.flink.api.java.functions.KeySelector)1 CEPCacheOptions (org.apache.flink.cep.configuration.CEPCacheOptions)1