Flink 1.20 使用自带jdbc source 操作步骤

1 添加pom.xml 依赖

复制代码
<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-connector-jdbc</artifactId>
    <version>3.3.0-1.20</version>
</dependency>

2 、操作mysql代码

复制代码
package com.zyb.flink.basic;
import com.zyb.flink.basic.bean.Student;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.connector.jdbc.core.datastream.source.JdbcSource;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


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

        //获取flink 上下文环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        JdbcSource<Student> jdbc = JdbcSource.<Student>builder()
                .setSql("select id,name,age from student")    
                .setDBUrl("jdbc:mysql://192.168.254.80:3306/pk_flink")
                .setUsername("root")
                .setPassword("123456")
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setTypeInformation(TypeInformation.of(Student.class))
                .setResultExtractor(rs->{
                    Student student = new Student();
                    student.setId(rs.getInt("id"));
                    student.setName(rs.getString("name"));
                    student.setAge(rs.getInt("age"));
                    return student;
                })
                .build();

        DataStreamSource<Student> source = env.fromSource(
                jdbc,
                WatermarkStrategy.noWatermarks(), // JDBC 源通常不使用事件时间水印,可以使用 NoWatermarks
                "JDBC-Source"
        );

        source.print();


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

3、测试结果

相关推荐
祭曦念3 小时前
MySQL基础运维:日志基础之慢查询日志与错误日志 | 作用、配置与查看方法全实战
运维·mysql·adb
陆业聪4 小时前
2026 年还在靠「感觉」调性能?Android Profiler 这样用才对
android·人工智能·性能优化
秋94 小时前
windows中下载并部署mysql-8.0.44-winx64详细过程
windows·mysql·adb
草莓熊Lotso4 小时前
MySQL 多表连接查询实战:内连接 + 外连接
android·运维·数据库·c++·mysql
草莓熊Lotso4 小时前
Linux 进程信号深度解析(下):信号的保存、阻塞与捕捉
android·linux·运维·服务器·数据库·c++·性能优化
运维老曾6 小时前
Flink MySQL source 自定义开发步骤
大数据·mysql·flink
小羊子说14 小时前
Android系统中 socketpair 的源码解读与应用分析小结
android·java
FL4m3Y4n15 小时前
MySQL索引原理与SQL优化
android·sql·mysql