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

因为如下:

测试结果如下:

相关推荐
大大大大晴天20 小时前
Flinksql内置函数不够用?一文弄懂UDF
flink
大大大大晴天3 天前
Hudi Metadata Table 与 Hive Sync (HMS)怎么选?
大数据
手可摘星辰7773 天前
一次线上FlinkCDC异常排查复盘
大数据·flink
大大大大晴天3 天前
Hudi技术内幕:Metadata Table原理与实践
大数据
大大大大晴天4 天前
Hudi技术内幕:深入解析Index索引机制
大数据
阿里云大数据AI技术4 天前
Flink Forward Asia 2026 深圳启幕:Agentic Streaming for AI,开启实时智能新范式
大数据·flink
SelectDB4 天前
阶跃星辰基于 SelectDB 构建 PB 级 Agent 可观测平台
大数据·数据库·aigc
tonyabasy5 天前
Flink 实时数仓开发实战:SQL中也能做到资源精细化管理
flink
大大大大晴天6 天前
浅聊Flink实时关联计算的不适用场景
flink
大大大大晴天7 天前
深入解析 Flink Kafka Connector:原理、配置与最佳实践
flink