如果在5秒内在Apache flink中发现5次相同的数据,则生成CEP事件

时间:2019-06-27 04:35:38

标签: apache-flink flink-streaming flink-cep

如果我们发现字符串消息在 5 秒内连续 5 次以' a '字符开头,则我需要生成CEP事件。

为此,我编写了一个类 CEPCharEventPublisher.java ,该类会将字符串消息(如下已发布的消息)发布到kafka主题“ charEvent '

已发布的消息:

b; date- 2019-06-27 09:05:09.605
a; date- 2019-06-27 09:05:10.160
c; date- 2019-06-27 09:05:10.661
b; date- 2019-06-27 09:05:11.162
c; date- 2019-06-27 09:05:11.669
b; date- 2019-06-27 09:05:12.175
b; date- 2019-06-27 09:05:12.675
b; date- 2019-06-27 09:05:13.176
a; date- 2019-06-27 09:05:13.676
c; date- 2019-06-27 09:05:14.176
b; date- 2019-06-27 09:05:14.677
b; date- 2019-06-27 09:05:15.177
b; date- 2019-06-27 09:05:15.678
c; date- 2019-06-27 09:05:16.178
a; date- 2019-06-27 09:05:16.679
c; date- 2019-06-27 09:05:17.179
c; date- 2019-06-27 09:05:17.680
c; date- 2019-06-27 09:05:18.180
c; date- 2019-06-27 09:05:18.681
c; date- 2019-06-27 09:05:19.181
c; date- 2019-06-27 09:05:19.681
a; date- 2019-06-27 09:05:20.182
c; date- 2019-06-27 09:05:20.682
b; date- 2019-06-27 09:05:21.182
c; date- 2019-06-27 09:05:21.682
b; date- 2019-06-27 09:05:22.183
a; date- 2019-06-27 09:05:22.683
b; date- 2019-06-27 09:05:23.184
a; date- 2019-06-27 09:05:23.684
c; date- 2019-06-27 09:05:24.184
b; date- 2019-06-27 09:05:24.685
b; date- 2019-06-27 09:05:25.186
c; date- 2019-06-27 09:05:25.687
b; date- 2019-06-27 09:05:26.187
a; date- 2019-06-27 09:05:26.687
a; date- 2019-06-27 09:05:27.188
a; date- 2019-06-27 09:05:27.688
b; date- 2019-06-27 09:05:28.188
b; date- 2019-06-27 09:05:28.688

现在我有一个使用者 CEPCharEventConsumer.java ,它将从Kafka主题 charEvent 中读取消息,并过滤以字符' a 开头的消息'。

然后我写了以下模式来生成CEP事件/警报,同时我们发现连续 5 消息,该消息以 5 < / strong>秒。

Pattern<String, String> pattern = Pattern.<String> begin("start")
                .times(5).greedy().where(new SimpleCondition<String>() {
                    private static final long serialVersionUID = -6301755149429716724L;

                    @Override
                    public boolean filter(String value) throws Exception {
                        return value.split(";")[0].equals("a");
                    }
                }).within(Time.seconds(5));

打印下面的 CEPCharEventConsumer.java 收到的以字符'a'开头的消息。

2> a; date- 2019-06-27 09:05:10.160
1> a; date- 2019-06-27 09:05:13.676
3> a; date- 2019-06-27 09:05:16.679
2> a; date- 2019-06-27 09:05:20.182
3> a; date- 2019-06-27 09:05:22.683
1> a; date- 2019-06-27 09:05:23.684
3> a; date- 2019-06-27 09:05:26.687
1> a; date- 2019-06-27 09:05:27.188
1> a; date- 2019-06-27 09:05:27.688
1> a; date- 2019-06-27 09:05:29.198
2> a; date- 2019-06-27 09:05:30.199

1> a; date- 2019-06-27 09:05:33.703
1> a; date- 2019-06-27 09:05:35.203
3> a; date- 2019-06-27 09:05:36.705
2> a; date- 2019-06-27 09:05:38.207
1> a; date- 2019-06-27 09:05:39.709
2> a; date- 2019-06-27 09:05:40.209
3> a; date- 2019-06-27 09:05:40.728

打印的警报消息:

4> Found: a; date- 2019-06-27 09:05:26.687

在上面的消息“ 发现:a;日期-2019-06-27 09:05:26.687 ”中是警报消息。

我不明白flink如何在5秒内计算连续的5条消息。我认为那里有问题。

我正在附上源代码(GIT URLflink-cep-char-event。有人可以按照我的要求将其正确吗?

1 个答案:

答案 0 :(得分:1)

您基于CEP的应用程序似乎正确地报告了这5条消息

3> a; date- 2019-06-27 09:05:26.687
1> a; date- 2019-06-27 09:05:27.188
1> a; date- 2019-06-27 09:05:27.688
1> a; date- 2019-06-27 09:05:29.198
2> a; date- 2019-06-27 09:05:30.199

在5秒钟的间隔内发生。

您的processMatch中的PatternProcessFunction方法传递了一个Map<String, List<String>> match。在您的情况下,match.get("start")在模式(整个模式)的“开始”子句中返回5个匹配事件的列表。

因此要生成一个报告,该报告给出上次匹配事件而不是第一个事件的时间,请更改

String start = match.get("start").get(0);
out.collect("Found: " + start);

String last = match.get("start").get(4);
out.collect("Found: " + last);