flink 基站与服务器长连接,每次连接和断开都会上报数据,统计过去一小时每个基站断开次数和时长

模拟生成数据

sql 复制代码
CREATE TABLE ods_station_log (
  base_station_id int,   -- 基站ID
  event_type      int,   -- 事件类型: connect/disconnect
  event_time      TIMESTAMP_LTZ(3), -- 事件时间
  WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND -- 允许5秒乱序
) WITH (
  'connector' = 'datagen',
  
  -- 基站ID生成规则(BS001 ~ BS100)
  'fields.base_station_id.kind' = 'random',
  'fields.base_station_id.min' = '1',
  'fields.base_station_id.max' = '2',
  
  -- 事件类型随机生成(50%概率为 connect/disconnect)
  'fields.event_type.kind' = 'random',
  'fields.event_type.min' = '0',
  'fields.event_type.max' = '1',
  
  -- 事件时间生成规则(模拟1小时数据,每秒10条)
  'fields.event_time.kind' = 'random',
  'fields.event_time.max-past' = '1000s',
  -- 控制数据生成速率
  'rows-per-second' = '1'
);

需求: 基站与服务器长连接,每次连接和断开都会上报数据,统计过去一小时每个基站断开次数和时长

思路:

sql 复制代码
CREATE TEMPORARY VIEW disconnect_records2 AS
SELECT
  base_station_id,
  connect_time,
  disconnect_time,
  TIMESTAMPDIFF(SECOND, connect_time, disconnect_time) AS duration
FROM ods_station_log
MATCH_RECOGNIZE (
  PARTITION BY base_station_id
  ORDER BY event_time
  MEASURES
    e_connect.event_time AS connect_time,
    e_disconnect.event_time AS disconnect_time
  ONE ROW PER MATCH
  AFTER MATCH SKIP PAST LAST ROW
  PATTERN (e_connect e_disconnect) 
  DEFINE
    e_connect AS e_connect.event_type = 1,
    e_disconnect AS e_disconnect.event_type = 0
);
相关推荐
倔强的石头1064 分钟前
【Linux指南】用户与系统基础操作
linux·运维·服务器
好多知识都想学17 分钟前
Centos 7 服务器部署多网站
linux·服务器·centos
藥瓿亭1 小时前
K8S认证|CKS题库+答案| 10. Trivy 扫描镜像安全漏洞
linux·运维·服务器·云原生·容器·kubernetes·cks
试剂界的爱马仕1 小时前
TCA 循环中间体如何改写肝损伤命运【AbMole】
大数据·人工智能·科技·机器学习·ai写作
leo__5201 小时前
在Ubuntu中设置开机自动运行(sudo)指令的指南
服务器·ubuntu·postgresql
Leo.yuan2 小时前
数据湖是什么?数据湖和数据仓库的区别是什么?
大数据·运维·数据仓库·人工智能·信息可视化
rui锐rui2 小时前
大模型模型部署和暴露接口
linux·运维·服务器
Clownseven2 小时前
云防火墙(安全组)配置指南:从入门到精通端口开放 (2025)
服务器·安全
hao_wujing3 小时前
基于梯度的中毒攻击
大数据·人工智能
码农101号12 小时前
Linux中shell编程表达式和数组讲解
linux·运维·服务器