Flink doesn‘t support ENFORCED mode for PRIMARY KEY constraint

flinkCDC感知mysql中表的数据的变化的时候,准备采用flinksql来实现,实现代码如下:

java 复制代码
package com.bigdata.cdc;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

/**
 * @基本功能:
 * @program:FlinkProject
 * @author: 闫哥
 * @create:2025-06-13 11:01:11
 **/
public class CdcSQLTest {

    public static void main(String[] args) throws Exception {

        //1. env-准备环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        env.setParallelism(1);
        // 获取tableEnv对象
        // 通过env 获取一个table 环境
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);

        //2. 创建表对象
        //3. 编写sql语句
        //4. 将Table变为stream流
        tenv.executeSql("CREATE TABLE user_info2 (\n" +
                " id INT NOT NULL primary key,\n" +
                " name STRING,\n" +
                " age int\n" +
                ") WITH (\n" +
                " 'connector' = 'mysql-cdc',\n" +
                " 'hostname' = 'bigdata01',\n" +
                " 'port' = '3306',\n" +
                " 'username' = 'root',\n" +
                " 'password' = '123456',\n" +
                " 'scan.startup.mode' = 'latest-offset', " +
                " 'database-name' = 'cdc_test',\n" +
                " 'table-name' = 'user_info'\n" +
                ")");

        tenv.executeSql("select * from user_info2").print();

        Table table = tenv.sqlQuery("select * from user_info2");
        DataStream<Tuple2<Boolean, Row>> retractStream = tenv.toRetractStream(table, Row.class);
        retractStream.print();


        //5. execute-执行
        env.execute();
    }
}

以上代码会报如下错误:

java 复制代码
Exception in thread "main" org.apache.flink.sql.parser.error.SqlValidateException: Flink doesn't support ENFORCED mode for PRIMARY KEY constraint. ENFORCED/NOT ENFORCED controls if the constraint checks are performed on the incoming/outgoing data. Flink does not own the data therefore the only supported mode is the NOT ENFORCED mode

修改方案:

id INT NOT NULL primary key 修改为 id INT NOT NULL PRIMARY KEY NOT ENFORCED

因为如下:

测试结果如下:

相关推荐
苏州IT威翰德3 小时前
算力资产“续命”专家:苏州威翰德科技赋能NVIDIA高端AI芯片芯片级维修
大数据·人工智能
Miao121313 小时前
某海外住宿平台如何在大规模场景下实现指标一致性:Minerva 指标平台实践
大数据·数据库·人工智能
SeaTunnel3 小时前
从 Python Script 地狱到标准化数据集成框架
大数据·开发语言·python·程序员·代码·seatunnel
爱自由的代码工4 小时前
游戏沉默用户怎么激活?一套可执行的步骤、指标与复盘方法
大数据·游戏·游戏发行·游戏运营·游戏分发
miaowu3575 小时前
AI智能体推动数字化转型:从流程自动化到决策辅助的完整路线图
大数据·人工智能·自动化
互联网江湖6 小时前
珞石机器人:向左科技股,向右零部件制造商?
大数据·人工智能
CRMEB系统商城6 小时前
开源自建还是SaaS订阅?算一笔3年经济账
java·大数据·开发语言·开源
Leo.yuan6 小时前
数据仓库建设怎么做?从源数据到分析报表全流程讲清
大数据·分布式·spark
pftn8 小时前
从0到1搭建具身智能数据飞轮:Ray + Kafka + LanceDB 全链路实战
大数据
Web3_Daisy9 小时前
什么是 Robinhood Chain?
大数据·人工智能·web3·区块链