- 普通函数类
指定了输入类型与返回类型,功能同map算子相似 - 富函数类
涉及到了资源相关,例如连接数据库与关闭数据库等操作写在代码中
代码中在建立连接的时候输出了一句话,执行程序可以看到,打印该话只执行了一次 - 底层处理函数api
添加了时间语义,触发器概念,灵活使用触发器,实现时间间隔内的各种操作
package com.uu.two
import java.sql
import java.sql.DriverManager
import com.uu.two.utils.DateTrans
import org.apache.flink.api.common.functions.{MapFunction, RichMapFunction}
import org.apache.flink.api.common.state.ValueStateDescriptor
import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.KeyedProcessFunction
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.util.Collector
case class Humman(id:Long,name:String,sex:String)
object FuncOne {
def main(args: Array[String]): Unit = {
val see = StreamExecutionEnvironment.getExecutionEnvironment
see.setParallelism(1)
import org.apache.flink.api.scala._
val value = see.socketTextStream("152.136.136.15", 9999)
val phoneMap = value.map(x => {
val arrayPhone = x.split(" ")
new Humman(arrayPhone(0).trim.toLong, arrayPhone(1), arrayPhone(2))
})
phoneMap
.map(new MyRichFunc)
.print()
see.execute("FuncOne")
}
class MyMapFunc extends MapFunction[Humman,String]{
override def map(value: Humman): String = {
"id:"+value.id+
"名字:"+value.name+
"性别:"+value.sex
}
}
class MyRichFunc extends RichMapFunction[Humman,String]{
var conn:sql.Connection = _
override def open(parameters: Configuration): Unit = {
conn = DriverManager.getConnection("jdbc:mysql://localhost/sqltest","root","123")
println("建立同数据库的连接")
}
override def map(value: Humman): String = {
var addr:String = ""
val id = value.id
val statement = conn.prepareStatement("select address from student3 where id = " + id)
val result = statement.executeQuery()
if(result.next()){
addr = result.getString(1)
}
statement.close()
"id:"+value.id+
"名字:"+value.name+
"性别:"+value.sex+
"地址:"+addr
}
override def close(): Unit = {
conn.close()
}
}
class MoniterFunc() extends KeyedProcessFunction[String,Humman,String]{
lazy val timeState = getRuntimeContext.getState(new ValueStateDescriptor[Long]("time", classOf[Long]))
override def processElement(value: Humman, ctx: KeyedProcessFunction[String, Humman, String]#Context, out: Collector[String]): Unit = {
val current: Long = timeState.value()
if (value.sex == "man"){
val processTime: Long = ctx.timerService().currentProcessingTime()
val endTime = processTime + 5000L
ctx.timerService().registerProcessingTimeTimer(endTime)
timeState.update(endTime)
}
if(value.sex == "women"){
ctx.timerService().deleteProcessingTimeTimer(current)
timeState.clear()
}
}
override def onTimer(timestamp: Long, ctx: KeyedProcessFunction[String, Humman, String]#OnTimerContext, out: Collector[String]): Unit = {
val str = "触发时间:" + DateTrans.getStandTime(timestamp) + "key值" + ctx.getCurrentKey
out.collect(str)
timeState.clear()
}
}
}