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、测试结果

相关推荐
数智工坊24 分钟前
【ROS 2 全栈入门指南三】:Action、参数与Launch文件全链路指南
android·stm32·嵌入式硬件·学习·机器人
问心无愧05131 小时前
ctf show web入门109
android·前端·笔记
xinhuanjieyi1 小时前
Android 画板应用kotlin实现
android·开发语言·kotlin
故渊at1 小时前
第四板块:Android 输入系统与触控事件 | 第十六篇:按键分发与软键盘(IME)的窗口协同
android·软键盘·输入系统·触控事件·按键分发
故渊at1 小时前
第三板块:Android 图形渲染与窗口体系 | 第十四篇:View 绘制体系与 RenderThread 异步渲染
android·图形渲染·ui线程·renderthread·view体系
会Tk矩阵群控的小木2 小时前
小红书矩阵软件:基于Python+ADB的多设备批量管理自动化脚本实战
运维·python·adb·矩阵·自动化·新媒体运营·个人开发
Coffeeee2 小时前
准备升级到Android16,自适应布局应该如何适配
android·google·kotlin
神仙别闹2 小时前
基于 PHP + MySQL 图书库存管理系统
android·mysql·php
IT界的老黄牛2 小时前
手机 Chrome 远程调试实战:adb + DevTools,localhost 就是你的测试服
chrome·测试工具·adb