大数据(9g)FlinkCEP
admin
2024-03-15 19:49:05
0次

文章目录

  • 概述
  • 示例代码
    • 环境和依赖
    • Java代码
      • 上面代码可改成下面

概述

  • CEP
    Complex Event Processing:复合事件处理
    通过分析事件间的关系,从事件流中查询出符合要求的事件序列
  • 例如【切菜=>洗菜=>炒菜】3个事件按时间序串联,是正常的事件流
    当发现【切菜=>炒菜】忽略洗菜的事件流,可认为是异常事件

示例代码

环境和依赖

WIN10+JDK1.8+IDEA2021+Maven3.6.3
CEP额外依赖为flink-cep

881.14.62.121.18.24


org.apache.flinkflink-java${flink.version}org.apache.flinkflink-streaming-java_${scala.binary.version}${flink.version}org.apache.flinkflink-clients_${scala.binary.version}${flink.version}org.apache.flinkflink-runtime-web_${scala.binary.version}${flink.version}org.apache.flinkflink-cep_${scala.binary.version}${flink.version}

Java代码

监测 严格近邻的连续三次a的事件流

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;public class CepPractice {public static void main(String[] args) throws Exception {//创建环境StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);//添加数据源,确定水位线策略SingleOutputStreamOperator d = env.fromElements("c", "a", "a", "a", "a", "b", "a", "a").assignTimestampsAndWatermarks(WatermarkStrategy.forMonotonousTimestamps().withTimestampAssigner((element, recordTimestamp) -> 1L));//定义模式Pattern p = Pattern.begin("first").where(new SimpleCondition() {@Overridepublic boolean filter(String value) {return value.equals("a");}}).next("second").where(new SimpleCondition() {@Overridepublic boolean filter(String value) {return value.equals("a");}}).next("third").where(new SimpleCondition() {@Overridepublic boolean filter(String value) {return value.equals("a");}});//在流上匹配模型PatternStream patternStream = CEP.pattern(d, p);//使用select方法将匹配到的事件流取出patternStream.select((PatternSelectFunction) map -> {//Map的key是事件名称(上面的first、second和third)//Map的key对应的value是列表,储存匹配到的事件String first = map.get("first").toString();String second = map.get("second").toString();String third = map.get("third").toString();return first + "->" + second + "->" + third;}).print();//执行env.execute();}
}

打印结果

[a]->[a]->[a]
[a]->[a]->[a]

上面代码可改成下面

留意.times(3).consecutive()

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;import java.util.List;public class CepPractice2 {public static void main(String[] args) throws Exception {//创建环境StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);//添加数据源,确定水位线策略SingleOutputStreamOperator> d = env.fromElements(Tuple2.of("a", 1000L), Tuple2.of("a", 2000L), Tuple2.of("a", 3000L),Tuple2.of("a", 4000L), Tuple2.of("b", 5000L), Tuple2.of("a", 6000L)).assignTimestampsAndWatermarks(WatermarkStrategy.>forMonotonousTimestamps().withTimestampAssigner((element, recordTimestamp) -> element.f1));//定义模式Pattern, Tuple2> p = Pattern.>begin("=a").where(new SimpleCondition>() {@Overridepublic boolean filter(Tuple2 value) {return value.f0.equals("a");}}).times(3).consecutive(); //严格连续//在流上匹配模型PatternStream> patternStream = CEP.pattern(d, p);//使用select方法将匹配到的事件流取出patternStream.select((PatternSelectFunction, String>) map -> {//Map的key是事件名称(上面的first、second和third)//Map的key对应的value是列表,储存匹配到的事件List> ls = map.get("=a");String first = ls.get(0).f0;String second = ls.get(1).f0;String third = ls.get(2).f0;return String.join("=>", first, second, third);}).print();//执行env.execute();}
}

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...