ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```java package com.gosuncn; import org.apache.flink.api.common.functions.FlatMapFunction; import org.apache.flink.api.common.typeinfo.Types; import org.apache.flink.api.java.ExecutionEnvironment; import org.apache.flink.api.java.operators.AggregateOperator; import org.apache.flink.api.java.operators.DataSource; import org.apache.flink.api.java.operators.FlatMapOperator; import org.apache.flink.api.java.tuple.Tuple2; public class WordCountBatchJob { public static void main(String[] args) throws Exception { final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); DataSource<String> lines = env.readTextFile("C:\\Users\\root\\Desktop\\data.txt"); FlatMapOperator<String, Tuple2> wordAndOne = lines.flatMap((FlatMapFunction<String, Tuple2>) (line, out) -> { for (String word : line.split(" ")) { out.collect(Tuple2.of(word, 1)); } }).returns(Types.TUPLE(Types.STRING, Types.INT)); AggregateOperator<Tuple2> summed = wordAndOne.groupBy(0).sum(1); // summed.print(); summed.writeAsText("C:\\Users\\root\\Desktop\\out\\res.txt"); env.execute("WordCountBatchJob"); } } ```