• StreamX 部署 Flink Stream 应用


    创建工程

    在这里插入图片描述

    笔者工程名叫Flink-StreamX

    在 pom 文件中添加 Flink 相关依赖

        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <flink.version>1.13.6flink.version>
            <scala.binary.version>2.11scala.binary.version>
            <slf4j.version>1.7.30slf4j.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.apache.flinkgroupId>
                <artifactId>flink-javaartifactId>
                <version>${flink.version}version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>org.apache.flinkgroupId>
                <artifactId>flink-streaming-java_${scala.binary.version}artifactId>
                <version>${flink.version}version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>org.apache.flinkgroupId>
                <artifactId>flink-clients_${scala.binary.version}artifactId>
                <version>${flink.version}version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>org.apache.flinkgroupId>
                <artifactId>flink-runtime-web_${scala.binary.version}artifactId>
                <version>${flink.version}version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-apiartifactId>
                <version>${slf4j.version}version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
                <version>${slf4j.version}version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>org.apache.logging.log4jgroupId>
                <artifactId>log4j-to-slf4jartifactId>
                <version>2.14.0version>
                <scope>providedscope>
            dependency>
        dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-shade-pluginartifactId>
                    <version>3.2.4version>
                    <executions>
                        <execution>
                            <phase>packagephase>
                            <goals>
                                <goal>shadegoal>
                            goals>
                            <configuration>
                                <artifactSet>
                                    <excludes>
                                        <exclude>com.google.code.findbugs:jsr305exclude>
                                        <exclude>org.slf4j:*exclude>
                                        <exclude>log4j:*exclude>
                                    excludes>
                                artifactSet>
                                <filters>
                                    <filter>
                                        
                                        <artifact>*:*artifact>
                                        <excludes>
                                            <exclude>META-INF/*.SFexclude>
                                            <exclude>META-INF/*.DSAexclude>
                                            <exclude>META-INF/*.RSAexclude>
                                        excludes>
                                    filter>
                                filters>
                                <transformers combine.children="append">
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer">
                                    transformer>
                                transformers>
                            configuration>
                        execution>
                    executions>
                plugin>
            plugins>
        build>
    
    • 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
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    示例代码

    package com.apache.bigdata;
    
    import org.apache.flink.api.common.functions.FlatMapFunction;
    import org.apache.flink.api.common.functions.MapFunction;
    import org.apache.flink.api.java.functions.KeySelector;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.util.Collector;
    
    public class UnboundedWC {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
    
            env.socketTextStream("hadoop102", 9999)
                    .flatMap(new FlatMapFunction<String, String>() {
                        @Override
                        public void flatMap(String line, Collector<String> out) throws Exception {
                            for (String word : line.split(" ")) {
                                out.collect(word);
                            }
                        }
                    })
                    .map(new MapFunction<String, Tuple2<String, Long>>() {
                        @Override
                        public Tuple2<String, Long> map(String word) throws Exception {
                            return Tuple2.of(word, 1l);
                        }
                    })
                    .keyBy(new KeySelector<Tuple2<String, Long>, String>() {
                        @Override
                        public String getKey(Tuple2<String, Long> t) throws Exception {
                            return t.f0; // t._1
                        }
                    })
                    .sum(1)
                    .print();
                    
            env.execute();
        }
    }
    
    • 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

    代码推送到 gitee

    在 streamx 平台部署应用的时候要求代码最好部署在 git 平台,比如 github 或 gitee。作为国内用户我们选择比较稳定的 gitee。

    如果不会git/gitee的小伙伴,可以从基础学习一下,时间不长,一天足够。

    我的项目推送地址:https://gitee.com/luan_hao/Flink-StreamX/

    配置项目

    在这里插入图片描述
    在这里插入图片描述

    编译项目

    编译前:

    在这里插入图片描述
    在这里插入图片描述

    第一次编译需要的时间比较久, 因为需要下载许多的依赖。

    编译成功后:

    在这里插入图片描述

    在这里插入图片描述

    提交应用

    1)创建应用

    在这里插入图片描述

    2)配置应用

    在这里插入图片描述

    在这里插入图片描述

    3)上线应用

    在这里插入图片描述

    4)启动应用(注意先启动 socket: nc -lk 9999)

    启动成功的表现:

    在这里插入图片描述

    开始运行:

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在弹出的界面里点击Task Managers,然后点击正在运行的任务

    在这里插入图片描述

    [root@hadoop102 local]# nc -lk 9999
    spark kafka
    flink hadoop
    
    • 1
    • 2
    • 3

    再点击stdout即可查看输出:

    在这里插入图片描述

    Yarn 平台确认执行结果

    在这里插入图片描述

    成功。

  • 相关阅读:
    计算机毕业设计(附源码)python疫情综合管控系统平台
    剑指 Offer 2022/7/5
    JuiceFS 在多云存储架构中的应用 | 深势科技分享
    python--pip常用命令、国内PyPI镜像、使用pip安装第三方库
    【Numpy】np.linalg.norm() 的用法(求范数)
    C# redis通过stream实现消息队列以及ack机制
    UE4基础篇十二: 网络同步
    SCADA系统原理
    嵌入式Linux C多进程编程(五)——进程退出和进程的等待
    使用makecert.exe创建数字证书
  • 原文地址:https://blog.csdn.net/weixin_45417821/article/details/125896898