DataStream API
1.DataStream 概览
1.1 使用示例
package com.lyh.flink
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
object SocketWindowWordCount {
def main(args: Array[String]): Unit = {
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
val dStream: DataStream[String] = env.socketTextStream("localhost", 9999, '\n')
val windowWordCount = dStream.flatMap(w => w.split("\\s"))
.map(w => WordWithCount(w, 1))
.keyBy(0)
.timeWindow(Time.seconds(5))
.sum(1)
.setParallelism(1)
windowWordCount.print()
env.execute("Socket Window WordCount")
}
case class WordWithCount(word: String, count: Int)
}
启动并查看结果:
nc -lk 9999
hello word word
1> WordWithCount(hello,1)
2> W

最低0.47元/天 解锁文章
717

被折叠的 条评论
为什么被折叠?



