Java版WordCount[批处理]

x33g5p2x  于2021-03-14 发布在 Flink  
字(1.1k)|赞(0)|评价(0)|浏览(252)
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");
    }
}

相关文章