Flink DataStream创建执行环境的正确方式与细节问题

java 复制代码
package com.flink.DataStream.env;

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

public class flinkEnvDemo {
    public static void main(String[] args) throws Exception {
        //TODO 创建一个Flink的配置对象
        Configuration configuration = new Configuration();
        //默认是8081,我们改为8082
        configuration.set(RestOptions.BIND_PORT, "8082");
        //TODO 创建Flink的执行环境
        StreamExecutionEnvironment streamExecutionEnvironment = StreamExecutionEnvironment
                //.createLocalEnvironment()   //创建本地环境
                //.createRemoteEnvironment()  //远程环境
                //开发过程中直接使用,他会自动判断是本地集群还是远程环境
                //.getExecutionEnvironment();
                //.getExecutionEnvironment(configuration);
                .createLocalEnvironmentWithWebUI(configuration);  //不启动Flink集群也可以有Web UI
        //TODO 流批一体:代码api是同一套 可以指定为流(默认),也可以指定为批
        //TODO 一般不在代码中写死,提交时,指定参数 ---Dexeution.runtime-mode=STREAMING/BATCH
        streamExecutionEnvironment.setRuntimeMode(RuntimeExecutionMode.STREAMING);
        //TODO 创建FLink的source为socket数据源
        DataStreamSource<String> dataStreamSource = streamExecutionEnvironment.socketTextStream("localhost", 8888);
        //TODO 扁平化+转换+分组+聚合
        SingleOutputStreamOperator<Tuple2<String, Integer>> singleOutputStreamOperator = dataStreamSource.flatMap(
                        //使用Lamada表达式实现flatMap接口,当然也可以直接new一个匿名类实现,或者在外部单独定义一个接口实现
                        //泛型第一个是输入类型,第二个是输出类型
                        (String s, Collector<Tuple2<String, Integer>> collector) -> {
                            String[] splitResult = s.split(" ");
                            //循环遍历,将数据转为Tuple类型.spark的rdd算子map: _.map((_,1))
                            for (String word : splitResult) {
                                Tuple2<String, Integer> wordsAndOne = Tuple2.of(word, 1);
                                //使用采集器向下游发送数据
                                collector.collect(wordsAndOne);
                            }
                        })
                .returns(Types.TUPLE(Types.STRING, Types.INT))
                .keyBy(
                        (Tuple2<String, Integer> value) -> {
                            return value.f0;
                        }
                ).sum(1);
        //TODO Sink输出
        singleOutputStreamOperator.print();
        //TODO 执行Flink程序,需要抛异常
        streamExecutionEnvironment.execute("Flink Environment Demo");

        //TODO ......
        /**
         * 默认env.execute() 触发一个Flink Job
         * 一个main方法理论上可以指定多个execute,但是没有什么意义,因为指定到第一个就会阻塞掉
         * 但是Flink 提供了异步执行的方式,一个main方法里面executeAsync()的个数 = 生成的Flink Job数
         * */
        //streamExecutionEnvironment.executeAsync();//异步执行
    }
}
相关推荐
大大大大晴天5 小时前
浅聊维度建模中的星型模型与雪花模型
大数据
数字新视界10 小时前
能耗管理系统提升企业能源效率的实践与展望
大数据·人工智能·数据中心·机房管理·模块化机房
大大大大晴天10 小时前
别让金额越算越多:事实表与维度表设计避坑指南
大数据
如夏倾城12 小时前
入侵检测系统(IDS)原理、部署模式与 IPS/防火墙对比
大数据·网络·人工智能·网络安全·云原生·架构·kubernetes
wuyk55512 小时前
Spider-第20讲-综合实战可上线爬虫
大数据·爬虫·python·elasticsearch
培培说证12 小时前
大数据与会计专业性价比高的证书有哪些?
大数据
伞伞悦读15 小时前
【第90期】KNN分类回归流水线与K值对照的实验与排查
大数据·人工智能·python·深度学习·机器学习
老余说AI15 小时前
跨境电商、课程出海:六款多语言视频翻译工具选型指南
大数据·运维·人工智能
starzy199015 小时前
Structured Streaming 模块与编程模型:引擎内部到底由什么组成
大数据·hadoop·hdfs
Lansonli15 小时前
大数据Spark(九十二):Spark WebUI介绍
大数据·分布式·spark