-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlinkProgram.scala
83 lines (55 loc) · 2.41 KB
/
FlinkProgram.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package org.apache.flink.quickstart
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.GlobalWindows
import org.apache.flink.streaming.api.windowing.triggers.Trigger.TriggerContext
import org.apache.flink.streaming.api.windowing.triggers.{PurgingTrigger, Trigger, TriggerResult}
import org.apache.flink.streaming.api.windowing.windows.{GlobalWindow, Window}
object FlinkProgram {
def main(args: Array[String]) {
val count = 0
val port = 6066
val env = StreamExecutionEnvironment.getExecutionEnvironment
//Create streams for names and ages by mapping the inputs to the corresponding objects
val text = env.socketTextStream("localhost", port, '\n')
val allValues = text.map {_.split("[,]") filter { _.nonEmpty } }
val dataTyped:DataStream[(Int,Double,Double,Double,Double)] = allValues.map { x =>
(1, x(0).toDouble, x(1).toDouble, x(2).toDouble, x(3).toDouble)
}
val events:DataStream[Sensor] = dataTyped
.map(x => Sensor(x._1, x._2, x._3, x._4, x._5))
val windowed = events
.keyBy("stream_num")
.window(GlobalWindows.create())
.trigger(PurgingTrigger.of(new MarksTrigger[GlobalWindow]()))
.sum(1)
//.apply{x => x.toString}
windowed.map(x => x.toString)
.writeToSocket("localhost", 9099, new SimpleStringSchema())
env.execute("Scala SocketTextStreamWordCount Example")
}
class MarksTrigger[W <: Window] extends Trigger[Sensor,W] {
var count = 0
override def onElement(element: Sensor, timestamp: Long, window: W, ctx: TriggerContext): TriggerResult = {
//trigger is fired if average marks of a student cross 80
if(element.magnitude > 1.5) {
count = count +1
}
if(count >0 && count <= 500)
{
if(count < 500)count = count+1
else count = 0
TriggerResult.FIRE
}
else TriggerResult.CONTINUE
}
override def onProcessingTime(time: Long, window: W, ctx: TriggerContext): TriggerResult = {
TriggerResult.CONTINUE
}
override def onEventTime(time: Long, window: W, ctx: TriggerContext): TriggerResult = {
TriggerResult.CONTINUE
}
override def clear(window: W, ctx: TriggerContext) = ???
}
case class Sensor(stream_num : Int, X_axis : Double, Y : Double, Z : Double, magnitude : Double)
}