美文网首页
FLINK学习笔记(一)-- Word Count

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

作者: k_wzzc | 来源:发表于2018-11-07 21:38 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/uwubxqtx.html