Flink流批一体计算(23):Flink SQL之多流kafka写入多个mysql sink

目录

1. 准备工作

生成数据

创建数据表

2. 创建数据表

创建数据源表

创建数据目标表

3. 计算

WITH子句


1. 准备工作

生成数据

source kafka json 数据格式 :

topic  case_kafka_mysql:

{ "ts": "20201011","id": 8,"price_amt":211}

topic  flink_test_2:

{ "id": 8,"coupon_price_amt":100}

注意:针对双流中的每条记录都发触发

topic: case_kafka_mysql

docker exec -it 192d1369463a bash

bash-5.1# cd /opt/kafka_2.12-2.5.0/bin

bash-5.1# ./kafka-console-producer.sh --broker-list localhost:9092 --topic case_kafka_mysql

>{"ts": "20201011","id": 8,"price_amt":211}

topic: flink_test_2

docker exec -it 192d1369463a bash

bash-5.1# cd /opt/kafka_2.12-2.5.0/bin

bash-5.1# ./kafka-console-producer.sh --broker-list localhost:9092 --topic flink_test_2

>{"id": 8,"coupon_price_amt":100}

创建数据表

mysql 建表语句

CREATE TABLE `sync_test_2` (

  `id` bigint(11) NOT NULL AUTO_INCREMENT,

  `ts` varchar(64) DEFAULT NULL,

  `total_gmv` bigint(11) DEFAULT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `uidx` (`ts`) USING BTREE

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;



CREATE TABLE `sync_test_22` (

  `id` bigint(11) NOT NULL AUTO_INCREMENT,

  `ts` varchar(64) DEFAULT NULL,

  `coupon_ratio` double DEFAULT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `uidx` (`ts`) USING BTREE

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;

2. 创建数据表

创建数据源表
create table flink_test_2_1 (
  id BIGINT,
  ts VARCHAR,
  price_amt BIGINT,
  proctime AS PROCTIME ()
)
 with (
   'connector' = 'kafka',
   'topic' = 'case_kafka_mysql',
   'properties.bootstrap.servers' = '127.0.0.1:9092',
   'properties.group.id' = 'flink_gp_test2-1',
   'scan.startup.mode' = 'earliest-offset',
   'format' = 'json',
   'json.fail-on-missing-field' = 'false',
   'json.ignore-parse-errors' = 'true',
   'properties.zookeeper.connect' = '127.0.0.1:2181/kafka'
 );

create table flink_test_2_2 (
  id BIGINT,
  coupon_price_amt BIGINT,
  proctime AS PROCTIME ()
)
 with (
   'connector' = 'kafka',
   'topic' = 'flink_test_2',
   'properties.bootstrap.servers' = '127.0.0.1:9092',
   'properties.group.id' = 'flink_gp_test2-2',
   'scan.startup.mode' = 'earliest-offset',
   'format' = 'json',
   'json.fail-on-missing-field' = 'false',
   'json.ignore-parse-errors' = 'true',
   'properties.zookeeper.connect' = '127.0.0.1:2181/kafka'
 );

关键配置的说明:

json.fail-on-missing-field:在json缺失字段时是否报错

json.ignore-parse-errors:在解析json失败时是否报错

一般无法保证json格式,所以以上两个配置是比较重要的。

创建数据目标表
CREATE TABLE sync_test_2 (
                   ts string,
                   total_gmv bigint,
                   PRIMARY KEY (ts) NOT ENFORCED

 ) WITH (
   'connector' = 'jdbc',
   'url' = 'jdbc:mysql://127.0.0.1:3306/db01?characterEncoding=UTF-8',
   'table-name' = 'sync_test_2',
   'username' = 'root',
   'password' = 'Admin'
 );

CREATE TABLE sync_test_22 (
                   ts string,
                   coupon_ration bigint,
                   PRIMARY KEY (ts) NOT ENFORCED
 ) WITH (
   'connector' = 'jdbc',
   'url' = 'jdbc:mysql://127.0.0.1:3306/db01?characterEncoding=UTF-8',
   'table-name' = 'sync_test_2',
   'username' = 'root',
   'password' = 'Admin'
 );

3. 计算

一个作业中写入一个Sink或多个Sink

说明 写入多个Sink语句时,需要以BEGIN STATEMENT SET;开头,以END;结尾。

BEGIN STATEMENT SET;      --写入多个Sink时,必填。
INSERT INTO sync_test_2
SELECT
  ts,
  SUM(price_amt - coupon_price_amt) AS total_gmv
FROM
  (
    SELECT
      a.ts as ts,
      a.price_amt as price_amt,
      b.coupon_price_amt as coupon_price_amt
    FROM
      flink_test_2_1 as a
      LEFT JOIN flink_test_2_2 b on b.id = a.id
  )
GROUP BY ts;

INSERT INTO sync_test_22
SELECT
  ts,
  sum(coupon_price_amt)/sum(amount) AS coupon_ration
FROM
  (
    SELECT
      a.ts as ts,
      a.price_amt as price_amt,
      b.coupon_price_amt as coupon_price_amt
    FROM
      flink_test_2_1 as a
      LEFT JOIN flink_test_2_2 b on b.id = a.id
  )
GROUP BY ts;;
END;      --写入多个Sink时,必填。

WITH子句

WITH提供了一种编写辅助语句以用于更大的查询的方法。这些语句通常被称为公共表表达式(CTE),可以被视为定义仅针对一个查询存在的临时视图。

改写上述查询:

BEGIN STATEMENT SET;      --写入多个Sink时,必填。
with orders_with_coupon AS (
    SELECT
      a.ts as ts,
      a.price_amt as price_amt,
      b.coupon_price_amt as coupon_price_amt
    FROM
      flink_test_2_1 as a
      LEFT JOIN flink_test_2_2 b on b.id = a.id
)

INSERT INTO sync_test_2
SELECT
  ts,
  SUM(price_amt - coupon_price_amt) AS total_gmv
FROM orders_with_coupon
GROUP BY ts;

INSERT INTO sync_test_22
SELECT
  ts,
  coupon_price_amt/price_amt AS coupon_ration
FROM orders_with_coupon
GROUP BY ts;;
END;      --写入多个Sink时,必填。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-06 07:20:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-06 07:20:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-06 07:20:01       82 阅读
  4. Python语言-面向对象

    2023-12-06 07:20:01       91 阅读

热门阅读

  1. 第18关 K8s数据安全无忧——持久化存储详解

    2023-12-06 07:20:01       85 阅读
  2. Ubuntu22 安装 mysql5.7 过程记录

    2023-12-06 07:20:01       54 阅读
  3. Redis使用Lua脚本

    2023-12-06 07:20:01       62 阅读
  4. ubuntu 修改系统时间,解决更新软件报错问题

    2023-12-06 07:20:01       70 阅读
  5. Flink入门之DataStream API及kafka消费者

    2023-12-06 07:20:01       63 阅读
  6. golang 实现和linephone的功能

    2023-12-06 07:20:01       52 阅读
  7. Kafka Schema介绍

    2023-12-06 07:20:01       59 阅读
  8. Kotlin 中的 var 和 val:选择正确的变量声明

    2023-12-06 07:20:01       54 阅读
  9. 比较 Apache Hive 和 Spark

    2023-12-06 07:20:01       44 阅读
  10. JeecgBoot 框架升级 Spring Boot 3.1.5

    2023-12-06 07:20:01       58 阅读
  11. html简单的表格制作

    2023-12-06 07:20:01       61 阅读