FlinkCEP(01)--普通数据源

CEP(Complex Event Processing)就是在无界事件流中检测事件模式
步骤分为四步
1、构建数据源
2、构建规则
3、Pattern上进行过滤
4、结果处理

多种模式:
1、严格连续模式
2、松散连续
3、不确定的松散连续

        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-scala_2.11</artifactId>
            <version>${flink.version}</version>
        </dependency>
        
       <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-cep-scala_2.11</artifactId>
            <version>${flink.version}</version>
        </dependency>

需求1:用户点击完立刻购买的信息

import org.apache.flink.api.scala._
import org.apache.flink.cep.{CEP, PatternSelectFunction}
import org.apache.flink.cep.pattern.Pattern
import org.apache.flink.cep.pattern.conditions.SimpleCondition
import org.apache.flink.streaming.api.datastream.DataStream
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment

object Model02 {
 
 def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)

    val input: DataStream[Event] = env.fromElements(
      Event(1, "click", 1.0),
      Event(1, "buy", 2.0),
      Event(1, "show", 3.0),
      Event(2, "click", 4.0),
      Event(4, "show", 5.0),
      Event(6, "middle", 6.0),
      Event(2, "buy", 3.0),
      Event(2, "buy", 42.0),
      Event(3, "show", 1.0)
    )

    val pattern: Pattern[Event, Event] = Pattern.begin[Event]("start")
      .where(new SimpleCondition[Event] {
        override def filter(e: Event): Boolean = {
          e.action.equals("click")
        }
      })
      .next("end").subtype[Event](classOf[Event])
      .where(new SimpleCondition[Event] {
        override def filter(e: Event): Boolean = {
          e.action.equals("buy")
        }
      })
    
    val patternStream = CEP.pattern(input, pattern)

    patternStream.select(new PatternSelectFunction[Event, String] {
      override def select(map: java.util.Map[String, java.util.List[Event]]): String = {
        val click: Event = map.get("start").iterator().next()
        val buy: Event = map.get("end").iterator().next()
        s"uid: ${click.uid}, action: ${buy.action}, time: ${buy.time}"
      }
    }).print()

    env.execute("cep demo")
  }

  case class Event(uid: Int, action: String, time: Double)
}

// 结果:
// name: 1, click: buy, time: 2.0

需求2:用户点击完后购买的信息(next 改为followedBy)


 val pattern: Pattern[Event, Event] = Pattern.begin[Event]("start")
      .where(new SimpleCondition[Event] {
        override def filter(e: Event): Boolean = {
          e.action.equals("click")
        }
      })
      .followedBy("end").subtype[Event](classOf[Event])
      .where(new SimpleCondition[Event] {
        override def filter(e: Event): Boolean = {
          e.action.equals("buy")
        }
      })

// 结果:
// name: 1, click: buy, time: 2.0
// name: 2, click: buy, time: 3.0

需求2:用户点击完后所有的购买的信息(next 改为followedByAny)


 val pattern: Pattern[Event, Event] = Pattern.begin[Event]("start")
      .where(new SimpleCondition[Event] {
        override def filter(e: Event): Boolean = {
          e.action.equals("click")
        }
      })
      .followedByAny("end").subtype[Event](classOf[Event])
      .where(new SimpleCondition[Event] {
        override def filter(e: Event): Boolean = {
          e.action.equals("buy")
        }
      })

// 结果:
// name: 1, click: buy, time: 2.0
// name: 1, click: buy, time: 3.0
// name: 2, click: buy, time: 3.0
// name: 1, click: buy, time: 42.0
// name: 2, click: buy, time: 42.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值