Flink 1.18.1的基本使用

系统示例应用
shell 复制代码
/usr/local/flink-1.18.1/bin/flink run /usr/local/flies/streaming/SocketWindowWordCount.jar --port 9010
shell 复制代码
nc -l 9010
asd asd sdfsf sdf sdfsdagd sdf

单次统计示例工程
shell 复制代码
cd C:\Dev\IdeaProjects


mvn archetype:generate -DarchetypeGroupId=org.apache.flink -DarchetypeArtifactId=flink-quickstart-java -DarchetypeVersion=1.18.1
shell 复制代码
 Define value for property 'groupId':
 Define value for property 'artifactId':
 Define value for property 'version' 1.0-SNAPSHOT: :
 Define value for property 'package' : :

 com.edu
 flink-example
 1.0.0
 com.edu.flink
java 复制代码
package com.edu.flink;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.time.Duration;


public class WindowWordCount {

    public static void main(String[] args) throws Exception {
        //设置运行时环境
        StreamExecutionEnvironment env =
                StreamExecutionEnvironment.getExecutionEnvironment();

        //设置输入流,并执行数据流的处理和转换
        env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);
        DataStream<Tuple2<String, Integer>> dataStream = env
                .socketTextStream("192.168.18.128", 9000)
                .flatMap(new Splitter())
                .keyBy(0)
                .timeWindow(Time.seconds(5))
                .sum(1);
        dataStream.assignTimestampsAndWatermarks(
                WatermarkStrategy.forBoundedOutOfOrderness(Duration.ofSeconds(3))
        );

        //设置输出流
        dataStream.print();
        //执行程序
        env.execute("Window WordCount");
        System.out.print("finished...");
    }

    public static class Splitter implements FlatMapFunction<String, Tuple2<String,
            Integer>> {
        @Override
        public void flatMap(String sentence, Collector<Tuple2<String, Integer>> out)
                throws Exception {
            for (String word : sentence.split(" ")) {
                out.collect(new Tuple2<String, Integer>(word, 1));
            }
        }
    }

}
相关推荐
中科岩创3 小时前
某公园楼栋自由曲面薄壳结构自动化监测
大数据·网络·物联网·自动化
caihuayuan54 小时前
升级element-ui步骤
java·大数据·spring boot·后端·课程设计
zskj_zhyl6 小时前
数字康养新范式:七彩喜平台重构智慧养老生态的深度实践
大数据·人工智能·物联网
白码低代码6 小时前
橡胶制品行业质检管理的痛点 质检LIMS如何重构橡胶制品质检价值链
大数据·人工智能·重构·lims·实验室管理系统
Sui_Network7 小时前
Sui Basecamp 2025 全栈出击
大数据·游戏·web3·去中心化·区块链
开利网络7 小时前
开放的力量:新零售生态的共赢密码
大数据·运维·服务器·信息可视化·重构
奋斗者1号8 小时前
神经网络中之多类别分类:从基础到高级应用
大数据·神经网络·分类
Microsoft Word8 小时前
Hadoop架构再探讨
大数据·hadoop·架构
吴爃8 小时前
了解Hadoop
大数据·hadoop·分布式
caihuayuan58 小时前
Vue生命周期&脚手架工程&Element-UI
java·大数据·spring boot·后端·课程设计