FLINK学习笔记(一)-- Word Count

object WordCount {

def main(args: Array[String]): Unit = {

/**
  * flink 的运行模型
  * 1.DataSource
  * 2.Transformation
  * 3.DataSink
  */
// 获取环境,类似于spark context
val env = StreamExecutionEnvironment.getExecutionEnvironment

val ip = "***.***.***.***"
// 从socket创建DataStream
// socket  DataSource
val socketStream = env.socketTextStream(ip, 9000)

// Transformation  数据转换操作
val wordsStream = socketStream.flatMap(value => value.split("\\s+"))
  .map(value => (value, 1))

val keyValuePair1: KeyedStream[(String, Int), Tuple] = wordsStream.keyBy(0)
val countPair: DataStream[(String, Int)] = keyValuePair1.sum(1)

// Data Sink
countPair.print()

println("==================== 窗口计算 ==========================")
// 窗口计算 批次处理(每隔5秒计算一次)
val keyValuePair2 = wordsStream.keyBy(0).timeWindow(Time.seconds(5))
//
val countStream = keyValuePair2.sum(1)

countStream.print()
// 触发执行程序
env.execute(" Word Count ")

}
}

你可能感兴趣的:(FLINK学习笔记(一)-- Word Count)