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

}
相关推荐
YangYang9YangYan2 小时前
2027秋招:统计学专业如何把数据能力用在商品计划、会员运营与市场研究
大数据·人工智能
初願致夕霞2 小时前
Git 指南
大数据·git·elasticsearch
泡干脆面就番茄2 小时前
Hive分区表与分桶表:大数据查询提速的第一把钥匙
大数据·数据仓库·hive·hadoop
芷栀夏2 小时前
MonkeyCode本地部署后怎么远程用?从模型配置、VS Code到9999公网访问
大数据·elasticsearch·搜索引擎
updayday8542 小时前
印染产线电脑进程异常占用资源却要逐台排查 - Ping64 远程进程管理
大数据·网络·数据库·安全·智能路由器
chde2Wang2 小时前
hadoop部署错误-namenode address dfs.namenode.servicerpc-address not configured
java·大数据·hadoop
xz2024102****2 小时前
agent基础知识:function calling,MCP,和Skill
大数据·人工智能·agent
龙腾AI白云2 小时前
世界模型与智能体协同推理逻辑
大数据·数据库·知识图谱·pygame
远航计算机2 小时前
AI 答案里的链接,为什么开始分等级了?
大数据·人工智能·爬虫·aigc
silvamux_3 小时前
MCP 协议如何让大模型完成工具调用
大数据·服务器·elasticsearch·fastapi·vllm