大数据-玩转数据-Flink 自定义Sink(Mysql)

一、说明

如果Flink没有提供给我们可以直接使用的连接器,那我们如果想将数据存储到我们自己的存储设备中,mysql 的安装使用请参考
mysql-玩转数据-centos7下mysql的安装

创建表

sql 复制代码
CREATE TABLE `sensor` (
  `id` int(10)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

二、pom.xml 导入驱动

复制代码
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>

三、编写程序

sql 复制代码
package com.lyh.flink06;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class SinkMysql {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(2);
        DataStreamSource<Integer> dataStreamSource = env.fromElements(1, 2, 3, 4, 5, 6);
        KeyedStream<Integer, Integer> keyedStream = dataStreamSource.keyBy(new KeySelector<Integer, Integer>() {
            @Override
            public Integer getKey(Integer value) throws Exception {
                return value.intValue();
            }
        });
        keyedStream.addSink(new MysqlSink());
        env.execute();

    }
    public static class  MysqlSink extends RichSinkFunction<Integer>{

        private Connection sunbo;

        @Override
        public void open(Configuration parameters) throws Exception {
            Class.forName("com.mysql.cj.jdbc.Driver");
            sunbo = DriverManager.getConnection("jdbc:mysql://192.168.220.100:3306/test?useSSL=false", "sunbo", "Mysql123456#");

        }

        @Override
        public void close() throws Exception {
            if (sunbo != null) {
                sunbo.close();
            }
        }

        @Override
        public void invoke(Integer value, Context context) throws Exception {
            String sql = "insert into sensor(id)values(?)";
            PreparedStatement ps = sunbo.prepareStatement(sql);
            ps.setInt(1,value.intValue());
            ps.execute();
            ps.close();

        }
    }
}

四、运行测试

相关推荐
清辞85328 分钟前
Coze从入门到实战---第一、二章
大数据·人工智能·学习·语言模型
TomatoStudy1 小时前
IT职业教育AI落地与实训体系建设复盘——以职坐标模式为例
大数据·人工智能
Java 码思客1 小时前
【ElasticSearch从入门到架构师】第1章:ElasticSearch 核心认知与行业定位
大数据·elasticsearch·搜索引擎
AI行业学习1 小时前
CC-Switch v3.16.1 官方下载 | 安装配置详细教程【2026.6.10】
java·开发语言·vue.js·python·mysql·eclipse·html
用户3074596982072 小时前
乐观锁与悲观锁
mysql
cui17875682 小时前
物业费收缴困局的破题之路:2026年社区商业逻辑的底层重构
大数据·数据库·人工智能
2501_933670792 小时前
大数据在校实训项目一般做什么类型内容
大数据
monsion2 小时前
Loop Engineering:你不再 prompt agent,而是设计 prompt agent 的系统
大数据·人工智能·prompt
AOwhisky2 小时前
学习自测与解析:MySQL第五、六、七期核心知识点详解
运维·数据库·笔记·学习·mysql·云计算
保卫大狮兄3 小时前
什么是WBS项目管理?WBS有哪些核心功能?
大数据·人工智能