Flink 添加自定义数据源实操

文章目录

    • 依赖
    • 代码部分
    • 初学者可能会遇见的问题

依赖

不再赘述

代码部分

//样例类
//用户行为
case class MarketingUserBehavior(userId: String, channel: String, behavior: String, timeStamp: Long)

//
object MarketAnalysis {
  def main(args: Array[String]): Unit = {
    //获取运行环境
    val env: StreamExecutionEnvironment= StreamExecutionEnvironment.getExecutionEnvironment
    //设置并行度
    env.setParallelism(1)
    //设置事件时间
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)

    //添加自定义数据源
    val source: DataStream[MarketingUserBehavior] =
      env.addSource(new EventSourceFunction()).assignAscendingTimestamps(_.timeStamp)

    source.print("...")

    //执行
    env.execute("app 推广")
  }


}
class EventSourceFunction() extends RichSourceFunction[MarketingUserBehavior]{
  //定义一个flag
  var running: Boolean = true
  override def cancel(): Unit = {
    running = false
  }

  override def run(ctx: SourceFunction.SourceContext[MarketingUserBehavior]): Unit = {
    val maxElements = Long.MaxValue
    var count = 0L

    while (running && count < maxElements){
      val behavior: Seq[String] = Seq("BUY", "DOWNLOAD", "SCAN", "INSTALL", "UNINSTALL" )
      val market: Seq[String] = Seq("JD_APP_STORE", "WEIBO", "MI_MALL", "BAIDU_STORE", "GOOGLE_STORE", "V_MALL" )
      val timeStamp: Long = System.currentTimeMillis()
      val userId: String = UUID.randomUUID().toString.replaceAll("_","")
      val random: Random = new Random()
      ctx.collect(MarketingUserBehavior(userId, market(random.nextInt(market.size)), behavior(random.nextInt(behavior.size)), timeStamp))

      TimeUnit.SECONDS.sleep(1)
      count += 1
    }
  }
}

初学者可能会遇见的问题

替换框选中的依赖为框框下面的依赖。
Flink 添加自定义数据源实操_第1张图片

1. Error:(32, 20) could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[com.jhon.commons.MarketingUserBehavior]
      env.addSource(new EventSourceFunction()).assignAscendingTimestamps(_.timeStamp)```



你可能感兴趣的:(flink)