Flink SQL 练习

-- 1、实时统计道路拥堵情况,
-- 统计最近15分钟,每隔1分钟统计一次,
-- 统计车流量和平均车速
-- 将统计结果保存到数据库中

-- 1、创建kafka source表
CREATE TABLE cars (
    card_id   String,-- 车牌号
    road_id   String,-- 道路编号
    city_id   String,-- 城市编号
    car_id    String,-- 卡口编号
    com_id    String,-- 摄像头编号
    fx       String,-- 方向
    county_id String,-- 区县
    ts BIGINT,-- 时间
    speed Double, -- 速度
    event_time as TO_TIMESTAMP(FROM_UNIXTIME(ts)) ,-- 将时间戳转换成时间对象
    -- 指定事件时间和水位线,水位线前移5秒
    WATERMARK FOR event_time AS event_time - INTERVAL '5' SECOND
) WITH (
  'connector' = 'kafka',
  'topic' = 'cars', -- 数据的topic
  'properties.bootstrap.servers' = 'master:9092,node1:9092,node2:9092', -- broker 列表
  'properties.group.id' = 'testGroup', -- 消费者组
  'scan.startup.mode' = 'earliest-offset', -- 读取数据的位置earliest-offset latest-offset
  'format' = 'csv' -- 读取数据的格式
);


-- 创建mysql sink表
CREATE TABLE road_flow_avg_speed (
    road_id STRING,
    win_start TIMESTAMP(3),
    win_end  TIMESTAMP(3),
    flow  BIGINT,
    avg_speed DOUBLE,
    PRIMARY KEY (road_id,win_start) NOT ENFORCED -- 主键
) WITH (
    'connector' = 'jdbc',
    'url' = 'jdbc:mysql://master:3306/student',
    'table-name' = 'road_flow_avg_speed',
    'username' ='root',
    'password' ='123456'
);

-- 在mysql中建表
CREATE TABLE road_flow_avg_speed (
    road_id varchar(10),
    win_start DATETIME ,
    win_end  DATETIME ,
    flow  BIGINT,
    avg_speed DOUBLE,
    PRIMARY KEY (road_id,win_start) -- 主键
);

-- 将查询结果保存到mysql
insert into road_flow_avg_speed
select 
    road_id,
    HOP_START(event_time, INTERVAL '1' MINUTE, INTERVAL '15' MINUTE) as win_start, -- 窗口开始时间
    HOP_END(event_time, INTERVAL '1' MINUTE, INTERVAL '15' MINUTE) as win_end, -- 窗口结束时间
    count(1) as flow,
    avg(speed) as avg_speed
from 
    cars
group by 
    road_id,
    -- 滑动的事件时间窗口
    HOP(event_time, INTERVAL '1' MINUTE, INTERVAL '15' MINUTE) ;

你可能感兴趣的:(Flink,1.15.2,flink,sql,大数据)