38、Flink 的 WindowAssigner 之 GlobalWindows 示例

1、注意

使用 GlobalWindows 需要自定义 Trigger,否则窗口中的数据不会被计算。

2、代码示例

bash 复制代码
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.GlobalWindows;
import org.apache.flink.streaming.api.windowing.triggers.Trigger;
import org.apache.flink.streaming.api.windowing.triggers.TriggerResult;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.util.Collector;

public class _05_WindowAssignerGlobal {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> input = env.socketTextStream("localhost", 8888);

        // 此窗口模式仅在指定了自定义的 trigger 时有用,否则计算不会发生,因为全局窗口没有天然的终点去触发其中积累的数据
        input
                .keyBy(e -> e)
                // 多并行 Task
                .window(GlobalWindows.create())
                .trigger(new Trigger<String, GlobalWindow>() {
                    @Override
                    public TriggerResult onElement(String s, long l, GlobalWindow globalWindow, TriggerContext triggerContext) throws Exception {
                        return null;
                    }

                    @Override
                    public TriggerResult onProcessingTime(long l, GlobalWindow globalWindow, TriggerContext triggerContext) throws Exception {
                        return null;
                    }

                    @Override
                    public TriggerResult onEventTime(long l, GlobalWindow globalWindow, TriggerContext triggerContext) throws Exception {
                        return null;
                    }

                    @Override
                    public void clear(GlobalWindow globalWindow, TriggerContext triggerContext) throws Exception {

                    }
                })
                .apply(new WindowFunction<String, String, String, GlobalWindow>() {
                    @Override
                    public void apply(String s, GlobalWindow globalWindow, Iterable<String> iterable, Collector<String> collector) throws Exception {
                        for (String res : iterable) {
                            collector.collect(res);
                        }
                    }
                })
                .print();

        env.execute();
    }
}
相关推荐
安达发公司6 分钟前
安达发|石油化工行业自动排产软件:驱动产业升级的核心引擎
大数据·人工智能·aps高级排程·aps排程软件·安达发aps·自动排产软件
摸鱼仙人~9 分钟前
跨文化范式迁移与数字经济重构:借鉴日本IP工业化经验构建中国特色现代文化产业体系深度研究报告
大数据·人工智能
DKunYu10 分钟前
2.分支管理
大数据·git·elasticsearch·搜索引擎·gitee
摸鱼仙人~17 分钟前
中国内需市场的战略重构与潜在增长点深度研究报告
大数据·人工智能
云飞云共享云桌面33 分钟前
非标自动化工厂的设计云桌面为什么要选云飞云智能共享云桌面?
大数据·运维·服务器·网络·自动化·负载均衡
做cv的小昊1 小时前
【TJU】信息检索与分析课程笔记和练习(6)英文数据库检索—web of science
大数据·数据库·笔记·学习·全文检索
五度易链-区域产业数字化管理平台1 小时前
基于产业大数据的产业园区精准招商解决方案:五度易链的全流程技术赋能逻辑
大数据
方渐鸿1 小时前
【2026】记录一次大数据请求时页面整体优化过程
大数据
天远云服1 小时前
Go语言高并发实战:集成天远手机号码归属地核验API打造高性能风控中台
大数据·开发语言·后端·golang
管理快车道1 小时前
连锁零售利润增长:我的实践复盘
大数据·人工智能·零售