object PhoneTest {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
val pro = new Properties()
pro.setProperty(“bootstrap.servers”,“spark1:9092”)
pro.setProperty(“zookeeper.connect”,“saprk1:2181”)
pro.setProperty(“group.id”,“phone”)
//记载配置
val consumer: FlinkKafkaConsumer011[String] = new FlinkKafkaConsumer011[String](“phone”, new SimpleStringSchema(), pro)
val ds: DataStream[String] = env.addSource(consumer)
//统计出5s内电话号码出现的总次数并打印到控制台
ds.filter(_.nonEmpty).flatMap(_.split(" ")).map((_,1)).keyBy(0).timeWindow(Time.seconds(5)).sum(1).print("5秒内")
//统计出5s内电话号码是偶数的电话并打印到控制台
ds.split(x=>{
var str = x.substring(10,11)
var int = str.toInt
if (int%2==0){
Seq("event")
}
else {
Seq("noevent")
}
}).select("event").print("偶数号码")
env.execute("phone:")
/* val process: DataStream[(String, Int)] = ds.process(new NumberIf).flatMap(_.split("\t")).map((_, 1)).keyBy(0).sum(1)
process.print("test")
process.getSideOutput(new OutputTag[String]("odd")).print("偶数:")*/
/*ds.filter(_.nonEmpty).map(data=>{
val dar: Array[String] = data.split("\t")
(dar(0).trim,1)
}).filter(x=>{x._1.substring(x._1.length).toInt%2==0}).keyBy(0)
.timeWindow(Time.seconds(5)).sum(1).print()*/
//8.统计出10s内同一电话号码出现次数超过2次及以上的电话号码并打印到控制台
ds.flatMap(_.split(" ")).map((_,1)).keyBy(0).timeWindow(Time.seconds(10))
.sum(1).filter(data=>data._2>=2).print("10秒内出现2次:")
//偶数车牌
}
class NumberIf extends ProcessFunction[String,String]{
lazy val evenOutPut = new OutputTagString
override def processElement(value: String, context: ProcessFunction[String, String]#Context, collector: Collector[String]): Unit = {
if (value.substring(10,11).toInt%2!=0){
context.output(evenOutPut,“奇数:”+value)
}else{
collector.collect(“偶数:”+value)
}
}
}
}