当前位置:网站首页>Flink1.11 Jdcb方式写mysql测试用例
Flink1.11 Jdcb方式写mysql测试用例
2022-07-26 22:40:00 【me凡】
本demo是计算窗口内wordCount然后写入mysql
//数据通过jdbc方式sink到mysql
windowCounts.addSink(JdbcSink.sink("replace into flink_test(words,nums) values(?,?)",
new JdbcStatementBuilder<Tuple2<String, Integer>>() {
@Override
public void accept(PreparedStatement ps, Tuple2<String, Integer> t) throws SQLException {
ps.setString(1,t.f0);
ps.setInt(2,t.f1);
System.out.println("数据为"+t.f0+":"+t.f1);
}
},
JdbcExecutionOptions.builder()
.withBatchSize(3) //此处需注意,默认的batchSize是5000
// .withBatchIntervalMs(3)
.build(),
new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
.withUrl("jdbc:mysql://localhost:3306/superset?serverTimezone=UTC")
.withDriverName("com.mysql.jdbc.Driver").withUsername("root").withPassword("123456")
.build()))
.setParallelism(1);//这个地方也需要注意,如果是并行度为2,
// 如果有三条数据,则可能不对写入到mysql,因为一个batchSize也是按线程划的,要一个线程的batchSize达到3才行我们通过JDBC方式sink到mysql,这里需要注意两个地方
1. 这里默认的batchSize是5000 ,如果不设置,可能会导致你的数据不会写入到msyql
JdbcExecutionOptions.builder()
.withBatchSize(3) //此处需注意,默认的batchSize是5000
// .withBatchIntervalMs(3)
.build(),源码如下
/**
* JDBC sink batch options.
*/
@PublicEvolving
public class JdbcExecutionOptions implements Serializable {
public static final int DEFAULT_MAX_RETRY_TIMES = 3;
private static final int DEFAULT_INTERVAL_MILLIS = 0;
public static final int DEFAULT_SIZE = 5000;
private final long batchIntervalMs;
private final int batchSize;
private final int maxRetries;
2. sink的并行度设置
sink的batchSize跟并行度有关,当某个线程的batchSize达到3时数据才会写入,所以要看我们的业务需求,比如写入topN,那设置一个并行度就行
这里我是想有3条数据就写入mysql,所以当我在本地测试而且没有设置并行度时,数据总是不能正确写入
边栏推荐
- 2020-12-20 99 multiplication table
- [SQL注入] 报错注入
- Inherit, inherit, inherit
- Install redis-7.0.4 in Linux system
- Two methods of automated testing XSS vulnerabilities using burpsuite
- [SQL注入] 扩展注入手法
- MySql - 如何确定一个字段适合构建索引?
- [watevrCTF-2019]Cookie Store
- Elaborate on the differences and usage of call, apply and bind 20211031
- Canal 介绍
猜你喜欢
随机推荐
[4.3 detailed explanation of Euler function]
Valueerror: the device should not be 'GPU', since paddepaddle is not compiled with CUDA
SSRF explanation and burp automatic detection SSRF
JSCORE day_05(7.6)
07 - 日志服务器的搭建与攻击
[漏洞实战] 逻辑漏洞挖掘
10 Web APIs
[qt] container class, iterator, foreach keyword
[acwing game 61]
DOM day_04(7.12)BOM、打开新页面(延迟打开)、地址栏操作、浏览器信息读取、历史操作
2022.7.9DAY601
Doris或StarRocks Jmeter压测
[RootersCTF2019]I_<3_Flask
[NCTF2019]SQLi
Promise基本用法 20211130
[Network Research Institute] attackers scan 1.6 million WordPress websites to find vulnerable plug-ins
select查询题目练习
[WUSTCTF2020]CV Maker
Install redis-7.0.4 in Linux system
[BJDCTF2020]EzPHP



![[CISCN2019 华北赛区 Day1 Web5]CyberPunk](/img/84/b186adc8becfc9b3def7dfd8e4cd41.png)
![[qt] container class, iterator, foreach keyword](/img/88/d9d5be096009b4e5baa0966e6f292c.jpg)
![[CTF攻防世界] WEB区 关于备份的题目](/img/af/b78eb3522160896d77d9e82f7e7810.png)

![[RootersCTF2019]I_<3_Flask](/img/69/1c77e45e939cf86bb75be8a6c42574.png)
![[BJDCTF2020]EzPHP](/img/be/a48a1a9147f1f3b21ef2d60fc1f59f.png)
