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

        }
    }
}

四、运行测试

相关推荐
玄斎8 小时前
MySQL 单表操作通关指南:建库 / 建表 / 插入 / 增删改查
运维·服务器·数据库·学习·程序人生·mysql·oracle
编程小Y9 小时前
MySQL 与 MCP 集成全解析(核心原理 + 实战步骤 + 应用场景)
数据库·mysql·adb
lvbinemail9 小时前
Grafana模板自动复制图表
数据库·mysql·zabbix·grafana·监控
weixin_4481199410 小时前
Datawhale Hello-Agents入门篇202512第1次作业
数据库·sql·mysql
皮皮林55110 小时前
有了开源的 MySQL,为什么还要选择 PostgreSQL?
mysql
老蒋新思维11 小时前
创客匠人视角:智能体重构创始人 IP,知识变现从 “内容售卖” 到 “能力复制” 的革命
大数据·网络·人工智能·tcp/ip·创始人ip·创客匠人·知识变现
Jackeyzhe11 小时前
Flink源码阅读:如何生成StreamGraph
flink
廋到被风吹走11 小时前
【数据库】【MySQL】分库分表策略 分类、优势与短板
数据库·mysql·分类
笨蛋少年派12 小时前
Flume数据采集工具简介
大数据
梦里不知身是客1112 小时前
spark中如何调节Executor的堆外内存
大数据·javascript·spark