Flink基础(入门程序WordCount实例)

           Flink作为当下流处理领域比较火爆的框架,下边来进行flink的第一个wordCount程序,让大家对于Flink有一个初步的认识:

  第一步: 使用idea创建maven项目(直接创建常规的maven项目)

第二步:向项目中引入flink的相关依赖


        8
        8
        1.13.0
        1.8
        2.12
        1.7.30
    

    
        
        
            org.apache.flink
            flink-java
            ${flink.version}
        
        
            org.apache.flink
            flink-streaming-java_${scala.binary.version}
            ${flink.version}
        
        
            org.apache.flink
            flink-clients_${scala.binary.version}
            ${flink.version}
        
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        
            org.apache.logging.log4j
            log4j-to-slf4j
            2.14.0
        
        
            org.apache.flink
            flink-connector-kafka_${scala.binary.version}
            ${flink.version}
        
        
            org.apache.flink
            flink-connector-jdbc_${scala.binary.version}
            ${flink.version}
        
        
            mysql
            mysql-connector-java
            5.1.47
        
    
    
        
            
                org.apache.maven.plugins
                maven-assembly-plugin
                3.0.0
                
                    
                        34
                        jar-with-dependencies
                    
                
                
                    
                        make-assembly
                        package
                        
                            single
                        
                    
                
            
        
    

  第三步:创建相关的类和包

Flink基础(入门程序WordCount实例)_第1张图片

 第四步:在项目根目录下创建input文件夹用于存放数据文件

Flink基础(入门程序WordCount实例)_第2张图片

 第五步:导入words.txt

hello world
hello flink
hello java

第六步:开始编写第一个flink程序(这里涉及到了flatMap转换算子,还有print输出算子,以及Tuple(元组)类型的转换,keyby(按键分区操作),sum(基本聚合算子)等),后续文章将为大家一 一讲解

package com.bw.wc;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;

/**
 * @BelongsProject: flink-day1
 * @BelongsPackage: com.bw.wc
 * @Author: fanshulei
 * @CreateTime: 2022/11/10 16:57
 * @Description:
 * @Version: 1.0
 */
public class StreamWordCount {
    public static void main(String[] args) throws Exception {
        // 1. 创建流式执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 2. 读取文件流
        DataStreamSource lineDss = env.readTextFile("input/words.txt");

        // 3. 转换数据格式
        SingleOutputStreamOperator> wordAndOne = lineDss
                .flatMap((String line, Collector words) -> {
                    Arrays.stream(line.split(" ")).forEach(words::collect);
                })
                .returns(Types.STRING)
                .map(word -> Tuple2.of(word, 1L))
                .returns(Types.TUPLE(Types.STRING, Types.LONG));
        // 4. 分组
        KeyedStream, String> wordAndOneKs = wordAndOne.keyBy(t -> t.f0);
        // 5. 求和
        SingleOutputStreamOperator> result = wordAndOneKs
                .sum(1);
        // 6. 打印
        result.print();
        // 7. 执行
        env.execute();
    }
}

打印输出结果

Flink基础(入门程序WordCount实例)_第3张图片

今天的flink入门程序分享就到这里,后续持续更新更多的flink学习教程(点关注不迷路哦)

你可能感兴趣的:(flink,flink,大数据)