Flink源码解析系列--DataStream JDBC-Sink

本文的Flink源码版本为: 1.15-SNAPSHOT,读者可自行从Github clone.

进行源码分析之前,首先看一下Flink DataStream JDBC-Sink的官方示例:

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env
	.fromElements(...)
	.addSink(JdbcSink.sink(
		"insert into books (id, title, author, price, qty) values (?, ?, ?, ?, ?)",
		(ps, t) -> {
			ps.setInt(1, t.id);
			ps.setString(2, t.title);
			ps.setString(3, t.author);
			ps.setDouble(4, t.price);
			ps.setInt(1, t.qty);
		},
		new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
			.withUrl(getDbMetadata().getUrl())
			.withDriverName(getDbMetadata().getDriverClass())
			.build()
	));	
env.execute();

可以看到,addSink传入的是JdbcSink类通过sink方法构建的。

public static  SinkFunction sink(
		String sql,
		JdbcStatementBuilder statementBuilder,
		JdbcConnectionOptions connectionOptions) {
	// 本质上调用的第2个sink方法
	retur

你可能感兴趣的:(Flink,Flink,JDBC,源码)