Flink word count入门

一下是一个简单的flink word count任务demo代码

pom配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>simple-demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>flink</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <flink.version>1.17.0</flink.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-java</artifactId>
            <version>${flink.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-clients</artifactId>
            <version>${flink.version}</version>
        </dependency>
    </dependencies>

</project>
任务编写
package com.xx.flink.task;

import org.apache.flink.api.common.functions.FlatMapFunction;
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;

/**
 * TODO DataSet API 实现 wordcount(不推荐)
 */
public class WordCountBatchDemo {
    public static void main(String[] args) throws Exception {
        // TODO 1. 创建执行环境
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        // TODO 2.读取数据:从文件中读取
        DataSource<String> lineDS = env.readTextFile("E:\\code-opensource\\simple-demo\\flink\\input\\word.txt");

        // TODO 3.切分、转换 (word,1)
        FlatMapOperator<String, Tuple2<String, Integer>> wordAndOne = lineDS.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                // TODO 3.1 按照 空格 切分单词
                String[] words = value.split(" ");
                // TODO 3.2 将 单词 转换为 (word,1)
                for (String word : words) {
                    Tuple2<String, Integer> wordTuple2 = Tuple2.of(word, 1);
                    //TODO 3.3 使用 Collector 向下游发送数据
                    out.collect(wordTuple2);
                }
            }
        });

        // TODO 4.按照 word 分组
        UnsortedGrouping<Tuple2<String, Integer>> wordAndOneGroupby = wordAndOne.groupBy(0);

        // TODO 5.各分组内聚合
        AggregateOperator<Tuple2<String, Integer>> sum = wordAndOneGroupby.sum(1); // 1是位置,表示第二个元素

        // TODO 6.输出
        sum.print();

    }
}
结果输出

over~~

相关推荐
W Y6 小时前
【架构-37】Spark和Flink
架构·flink·spark
ycsdn106 小时前
Caused by: org.apache.flink.api.common.io.ParseException: Row too short:
大数据·flink
晨航1 天前
Flink本地模式安装详解
大数据·flink
sf_www1 天前
flink 内存配置(五):网络缓存调优
大数据·网络·flink
我的K84091 天前
Flink滑动窗口(Sliding)中window和windowAll的区别
flink
inori12561 天前
FlinkSql读取外部Mysql和HBase数据库的方法(scala)
mysql·flink·hbase
小强签名设计2 天前
Flink CDC 同步 Mysql 数据
大数据·mysql·flink
牧竹子2 天前
FLINK单机版安装部署入门-1
大数据·flink
sf_www2 天前
flink 内存配置(四):内存调优和问题处理
大数据·flink
静听山水2 天前
Flink-Kafka-Connector
flink