当前位置:   article > 正文

Flink 1.16.0 wordCount示例,入门案例_flink-1.16.0

flink-1.16.0

环境准备

JAVA1.8

新建maven工程

引入依赖

<dependencies>

        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-java</artifactId>
            <version>1.16.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-clients</artifactId>
            <version>1.16.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-runtime</artifactId>
            <version>1.16.0</version>
        </dependency>

    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

示例代码

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.operators.UnsortedGrouping;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class FlinkApp {

   public static void main(String[] args) {

       // 创建执行环境
       ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();


       // 从文件中读取数据
       DataSource<String> lineDataSource = env.readTextFile("E:\\input\\words.txt");

       // 切分数据
       FlatMapOperator<String, Tuple2<String, Long>> wordAndOneTuple = lineDataSource.flatMap((String line, Collector<Tuple2<String, Long>> out) -> {

           String[] words = line.split(" ");
           for (String word : words) {
               out.collect(Tuple2.of(word, 1L));
           }

       }).returns(Types.TUPLE(Types.STRING, Types.LONG));

       UnsortedGrouping<Tuple2<String, Long>> wordAndOneGroup = wordAndOneTuple.groupBy(0);

       // 分组内进行聚合统计
       AggregateOperator<Tuple2<String, Long>> sum = wordAndOneGroup.sum(1);

       try {
           sum.print();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

文件内容示例:

hello world
hello flink
hello java
  • 1
  • 2
  • 3

运行结果:
在这里插入图片描述

项目结构

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/690386
推荐阅读
相关标签
  

闽ICP备14008679号