public class wordcount { public static void main(String[] args) throws Exception { ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); String inputPath = "D:\IDEAWorkspace\2017.3\bigData\Flink\src\main\resources\hello.txt"; DataSet<String> dataSource = env.readTextFile(inputPath); //对一批数据进行转换操作 DataSet<Tuple2<String, Integer>> result = dataSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() { @Override public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception { String[] words = s.split(" ", -1); for (String word : words) { collector.collect(new Tuple2<>(word, 1)); } } }) //已经有一批数据了,所以用groupBy进行分组 .groupBy(0)//根据元组的第一位分组 .sum(1);//对元组的第二位进行求和 result.print(); } }