大数据-玩转数据-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();

        }
    }
}

四、运行测试

相关推荐
程序猿小D2 小时前
[附源码+数据库+毕业论文+开题报告]基于Spring+MyBatis+MySQL+Maven+jsp实现的车辆运输管理系统,推荐!
java·数据库·mysql·spring·毕业设计·开题报告·车辆运输管理系统
zskj_zhyl2 小时前
从 “洗澡难” 到 “洗得爽”:便携智能洗浴机如何重塑生活?
大数据·人工智能·科技·生活
regret~4 小时前
【记录】Ubuntu20.04安装mysql
数据库·mysql
失重外太空啦8 小时前
Mysql练习
android·数据库·mysql
像风一样自由20208 小时前
Navicat操作指南:MySQL数据库配置与Todo应用部署
数据库·mysql·adb
村东头老张9 小时前
通过 Docker 安装 MySQL
mysql·docker·容器
会编程的林俊杰9 小时前
Buffer Pool
数据库·mysql
字节跳动数据平台10 小时前
火山引擎DataAgent:AI完成从“数据洞察”到“自主行动”质变
大数据
叁沐11 小时前
MySQL 16“order by”是怎么工作的?
mysql