2021年大数据Flink(十四):流批一体API Connectors JDBC
【摘要】
目录
Connectors
JDBC
代码演示
Connectors
JDBC
https://ci.apache.org/projects/flink/flink-docs-release-1.12/dev/connectors/jdbc.html
代码演示
package cn.itc...
目录
Connectors
JDBC
https://ci.apache.org/projects/flink/flink-docs-release-1.12/dev/connectors/jdbc.html
代码演示
-
package cn.itcast.connectors;
-
-
import lombok.AllArgsConstructor;
-
import lombok.Data;
-
import lombok.NoArgsConstructor;
-
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
-
import org.apache.flink.connector.jdbc.JdbcSink;
-
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
-
-
/**
-
* Author itcast
-
* Desc
-
*/
-
public class ConnectorsDemo_JDBC {
-
public static void main(String[] args) throws Exception {
-
//1.env
-
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
-
//2.Source
-
env.fromElements(new Student(null, "tonyma", 18))
-
//3.Transformation
-
//4.Sink
-
.addSink(JdbcSink.sink(
-
"INSERT INTO `t_student` (`id`, `name`, `age`) VALUES (null, ?, ?)",
-
(ps, s) -> {
-
ps.setString(1, s.getName());
-
ps.setInt(2, s.getAge());
-
},
-
new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
-
.withUrl("jdbc:mysql://localhost:3306/bigdata")
-
.withUsername("root")
-
.withPassword("root")
-
.withDriverName("com.mysql.jdbc.Driver")
-
.build()));
-
//5.execute
-
env.execute();
-
}
-
-
@Data
-
@NoArgsConstructor
-
@AllArgsConstructor
-
public static class Student {
-
private Integer id;
-
private String name;
-
private Integer age;
-
}
-
}
文章来源: lansonli.blog.csdn.net,作者:Lansonli,版权归原作者所有,如需转载,请联系作者。
原文链接:lansonli.blog.csdn.net/article/details/116245172
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)