Transform operation of Flink

org.apache.flink.api.common.functions.FilterFunction Import 
Import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment

Object TransformTest {
DEF main (args: the Array [String]): Unit = {
Val = StreamExecutionEnvironment the env. getExecutionEnvironment
env.setParallelism (. 1) // set the global degree of parallelism. 1

Import org.apache.flink.api.scala._
Val streamFromFile = env.readTextFile ( "sensor1.txt")

//. 1. The basic and simple conversion operator Aggregation operator
Val Datastream = streamFromFile.map (Data => {
Val dataArray is data.split = ( ",")
SensorReading (dataArray is (0) .trim, dataArray is (. 1) .trim.toLong, dataArray is (2) .trim. toDouble)
}
)
// Note that observation, to a data processing Flink is one, it will not only see the final result after the summation
dataStream.keyBy (0) .sum (2) .print ()
dataStream.keyBy ( "ID") SUM ( "temperature") Print () // second approach.
@ example: an output current sensor temperature plus the latest 10 , and the last time stamp is the time stamp data plus. 1
dataStream.keyBy (0) .reduce ((X, Y) => SensorReading (x.id, x.timestamp +. 1, y.temperature + 10)). Print () // X and y represent the current value and the new value

// 2. multiple sub-flow terms
// split split
Val SplitStream = dataStream.split (sensordata => {
IF (sensorData.temperature> 30)
Seq ( " High ")
the else
Seq (" Low ")
})
Val = High splitStream.select (" High ")
Val = splitStream.select Low (" Low ")
Val All splitStream.select = (" High "," Low ")
High .print ( "high temperature")
low.print("low temperature")
all.print ( "All")

// merge: connect and of Union
/ *
* 1. before the union of two types of streams must be the same, it may not be the same as Connect, after the adjustment again become coMap the same
* 2. connect only operate two streams, a plurality of operable union
* /
Val = High warning. Map (X => (x.id, x.temperature))
Val connectedStream = warning.connect (Low)
Val = connectedStream.map COMAP (
warningData => (warningData._1, warningData._2, "warning"),
safeData = > (safeData.id, "Safe")
)
coMap.print ()

Val = unionStream high.union (Low)
unionStream.print ()

// streamFromFile.map (Data => {
// len = data.split Val ( " , ")
// len (0) +" "+ len (. 1)
//}).

//自定义函数类
dataStream.filter(new MyFilter).print()

env.execute("transform test")
}
}

class MyFilter() extends FilterFunction[SensorReading] {
override def filter(t: SensorReading): Boolean = {
t.id.startsWith("sensor_1")
}
}

Guess you like

Origin www.cnblogs.com/wddqy/p/12172284.html