第一个 WordCount 程序
1. 批处理 WordCount
import org.apache.flink.api.scala._
// 批处理 WordCount
object BatchWordCount {
def main(args: Array[String]): Unit = {
// 1.创建执行环境
val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment
// 2.读取源文件
val inputPath = "/Users/zgl/Documents/IdeaProjects/FlinkTutorial/src/words.txt"
val inputDS: DataSet[String] = env.readTextFile(inputPath)
// 3. 统计计数
val resDS: AggregateDataSet[(String, Int)] = inputDS.flatMap(_.split(" ")).map((_, 1)).groupBy(0).sum(1)
// 4.打印结果
resDS.print()
}
}
2. 流处理 WordCount
import org.apache.flink.streaming.api.scala._
// 流处理 WordCount
object StreamWordCount {
def main(args: Array[String]): Unit = {
// 1.创建流处理环境
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
// 2.从指定的 IP:端口号 接收 socket 文本流
val inputDS: DataStream[String] = env.socketTextStream("localhost",7777)
// 3.统计计数
val resDS: DataStream[(String, Int)] = inputDS.flatMap(_.split(" ")).map((_, 1)).keyBy(0).sum(1)
// 4.打印结果
resDS.print()
// 5.启动 Executor,执行任务
env.execute("Stream WordCount Job...")
}
}