美文网首页
flink基础wordcount scala版

flink基础wordcount scala版

作者: DuLaGong | 来源:发表于2019-05-13 21:37 被阅读0次

import org.apache.flink.api.java.utils.ParameterTool

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment

import org.apache.flink.streaming.api.windowing.time.Time

object FlinkFirstAppScala {

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

// the host and the port to connect to

    var hostname:String ="localhost"

    var port:Int =0

    try {

val params = ParameterTool.fromArgs(args)

hostname = params.get("hostname","192.168.21.138")

port = params.getInt("port",9999)

}catch {

case e:Exception => {

System.err.println("No port specified. Please run 'SocketWindowWordCount " +

"--hostname <hostname> --port <port>', where hostname (localhost by default) and port " +

"is the address of the text server")

System.err.println("To start a simple text server, run 'netcat -l <port>' " +

"and type the input text into the command line")

return

      }

}

// get the execution environment

    val env=StreamExecutionEnvironment.getExecutionEnvironment

    import org.apache.flink.streaming.api.scala._

// get input data by connecting to the socket; parse the data, group it, window it, and aggregate the counts

    env.socketTextStream(hostname,port)

.flatMap(x=>x.split(" "))

.map(x=>WC(x,1))

.keyBy("word")

.timeWindow(Time.seconds(4),Time.seconds(2))

.sum("count")

.print

.setParallelism(1)

/** Data type for words with count */

    env.execute("FlinkFirstAppScala")

}

case class WC(word:String,count:Long)

}

相关文章

网友评论

      本文标题:flink基础wordcount scala版

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