Flink写入MySQL

本文介绍了如何在Flink流处理中使用JdbcSink将数据流写入MySQL数据库。首先,展示了所需的依赖配置,包括Flink JDBC connector和MySQL驱动。接着,通过创建一个简单的数据流并调用JdbcSink进行处理,详细说明了如何配置连接选项,如URL、驱动名、用户名和密码。最后,给出了创建MySQL数据库和表的SQL语句,以及执行代码后数据库中会存储的数据。
摘要由CSDN通过智能技术生成

MySQL Sink

在Flink流式数据处理中Sink常用的有Kafka,ES,MySQL,今天来看下,Flink中怎么使用JdbcSink将数据流写入MySQL中

配置

参考

FLINK 基础配置

POM

	<!--mysql-->
	<dependency>
	  <groupId>org.apache.flink</groupId>
	  <artifactId>flink-connector-jdbc_${scala.binary.version}</artifactId>
	  <version>${flink.version}</version>
	</dependency>
	<dependency>
	  <groupId>mysql</groupId>
	  <artifactId>mysql-connector-java</artifactId>
	  <version>8.0.29</version>
	</dependency>

模拟一些数据,处理后写入MySQL

package org.example.flink.level2;

/**
 * sink mysql
 */

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

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

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Event> stream = env.fromElements(
                new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 2000L),
                new Event("Alice", "./prod?id=100", 3000L),
                new Event("Alice", "./prod?id=200", 3500L),
                new Event("Bob", "./prod?id=2", 2500L),
                new Event("Alice", "./prod?id=300", 3600L),
                new Event("Bob", "./home", 3000L),
                new Event("Bob", "./prod?id=1", 2300L),
                new Event("Bob", "./prod?id=3", 3300L));

        stream.addSink(
                JdbcSink.sink(
                        "INSERT INTO clicks (user, url) VALUES (?, ?)",
                        (statement, r) -> {
                            statement.setString(1, r.user);
                            statement.setString(2, r.url);
                        },
                        new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                                .withUrl("jdbc:mysql://localhost:3306/flink")
                                .withDriverName("com.mysql.cj.jdbc.Driver")
                                .withUsername("root")
                                .withPassword("root")
                                .build()
                )
        );
        
        env.execute();
    }
}


创建库表SQL

create database if not exists flink;

use flink;

drop table if exists clicks;

create table if not exists clicks(
    user varchar(100) not null ,
    url varchar(100) not null
);

执行代码后,数据库就会有数据了

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BirdMan98

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值