scala编写flink批处理wordcount案例

tech2022-08-20  68

flink wordcount

添加maven依赖编写程序结果

添加maven依赖

<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-scala --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-scala_2.12</artifactId> <version>1.11.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-clients 从Flink1.11开始,移除了flink-streaming-java对flink-clients的依赖 --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-clients_2.12</artifactId> <version>1.11.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-scala --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-scala_2.12</artifactId> <version>1.11.1</version> </dependency>

编写程序

object FlinkWordCount { def main(args: Array[String]): Unit = { //获取批处理执行环境 val environment: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment val dataset: DataSet[String] = environment.readTextFile("./data/words") dataset.flatMap(_.split(" ")).map((_,1)).groupBy(0).sum(1).print() } }

结果

最新回复(0)