Flink CEP 对超时事件的处理

今天在群里面有个同学问了一个问题,一个通用场景,例如:用户2小时不评价订单,自动好评功能。

这种多长时间内没有收到第二个事件的问题,想了下以为可以用notFollowedBy 来解决,结果不行。
查了一下文档, 写了下面一个demo,记录一下,备忘

final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> dataStream = env.socketTextStream("172.16.150.189", 9000);

        Pattern<String, String> pattern = Pattern.<String>begin("first")
                .where(new SimpleCondition<String>() {
                    public boolean filter(String e) {
                        return e.contains("123");
                    }
                })
                .followedBy("second").where(new SimpleCondition<String>() {
                    @Override
                    public boolean filter(String value) throws Exception {
                        return value.contains("456");
                    }
                })
                .within(Time.seconds(3));

        PatternStream<String> ps = CEP.pattern(dataStream, pattern);
        ps.select(new PatternTimeoutFunction<String, String>() {
                      @Override
                      public String timeout(Map<String, List<String>> pattern, long timeoutTimestamp) throws Exception {
                          // 没有3s内评价的
                          System.out.println("===============>>>>>>>>>> behaviors: " + JsonHelper.toJson(pattern));
                          return null;
                      }
                  },
                new PatternSelectFunction<String, String>() {
                    @Override
                    public String select(Map<String, List<String>> pattern) throws Exception {
                        // 3s内评价了的
                        System.out.println("===============>>>>>>>>>> behavior size: " + pattern.size());
                        System.out.println("===============>>>>>>>>>> behaviors: " + JsonHelper.toJson(pattern));
                        return "";
                    }
                });

        env.execute("test pattern");

你可能感兴趣的:(flink)