Flink将数据写入到mysql

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcExecutionOptions;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * Author:panghu
 * Date:2022-05-29
 * Description: 写数据到mysql
 */
public class _18SinkToMySQLTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<String> streamSource = env.readTextFile("input/clicks.csv");
        // 将字符串封装为Event对象
        SingleOutputStreamOperator<Event> mapSource = streamSource.map(new MapFunction<String, Event>() {
            @Override
            public Event map(String line) throws Exception {
                String[] split = line.split(", ");
                Event event = new Event(split[0], split[1], Long.valueOf(split[2]));
                return event;
            }
        });

        mapSource.addSink(
                //参数1:sql
                JdbcSink.sink("INSERT INTO clicks(user, url) VALUES (?,?) ",
                        //参数2:statement占位符
                        (statement, data) -> {
                            statement.setString(1, data.user);
                            statement.setString(2, data.url);
                        },
                        //参数3:设置
                        JdbcExecutionOptions.builder()
                                .withBatchIntervalMs(200)   //批次提交周期:每200ms
                                .withBatchSize(1000)        //批次提交周期:每1000条
                                .withMaxRetries(5)          //批次提交周期,每5次
                                .build(),
                        new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                        .withUrl("jdbc:mysql://hadoop102:3306/test")    //mysql地址
                        .withDriverName("com.mysql.jdbc.Driver")
                        .withUsername("root")
                        .withPassword("000000")
                        .build()
                        )
        );

        env.execute();
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值