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));
            }
        }
    }

}
相关推荐
goTsHgo2 分钟前
Flink的数据流图中的数据通道 StreamEdge 详解
大数据·flink
SelectDB19 分钟前
Apache Doris & SelectDB 技术能力全面解析
大数据·数据库·程序员
笑傲码湖1 小时前
SkipList跳表:高效查找的利器
大数据
SimonKing2 小时前
MCP:大模型时代的智能导航系统
大数据·后端
CXH7282 小时前
hadoop伪分布式部署
大数据·hadoop·分布式
一个天蝎座 白勺 程序猿3 小时前
大数据(7.4)Kafka存算分离架构深度实践:解锁对象存储的无限潜能
大数据·架构·kafka
热心网友俣先生3 小时前
2025年认证杯C题超详细解题思路
大数据·数学建模
SmallFatMan4 小时前
智能客服系统中的意图识别与分类技术详解
大数据·人工智能·ai·数据挖掘·ai编程
.生产的驴10 小时前
SpringBoot 接口限流Lua脚本接合Redis 服务熔断 自定义注解 接口保护
java·大数据·数据库·spring boot·redis·后端·lua
2401_8242568613 小时前
Spark Core编程
大数据·分布式·spark