【Flink】Object Reuse 模式(Stream API) 性能优化 chain 数据重用 不深拷贝、对象重用

840 篇文章 855 订阅 ¥99.90 ¥299.90
本文介绍了Flink中Object Reuse模式如何提高性能,避免深拷贝带来的开销。默认情况下,Flink在Chained Operator间通过深拷贝传递数据,但Object Reuse可以通过复用对象提升效率。然而,启用该模式需要用户函数遵循特定规范,如不存储输入数据到State或修改已输出对象。文章通过分析源码,探讨了Object Reuse在Stream API和Batch API中的差异,以及与watermark的关联,揭示了深拷贝在某些场景下的必要性,并分享了遇到的深拷贝陷阱。
摘要由CSDN通过智能技术生成


在这里插入图片描述

1. 概述

在这里插入图片描述

昨天在界面上看到一句话,主管让加上去,于是我去查查这个参数有什么用

官网:https://ci.apache.org/projects/flink/flink-docs-stable/dev/execution_configuration.html

enableObjectReuse() / disableObjectReuse() By default, objects are not reused in Flink. Enabling the object reuse mode will instruct the runtime to reuse user objects for better performance. Keep in mind that this can lead to bugs when

Apache Flink是一个分布式处理流式数据的开源框架,而TiDB是一个分布式SQL数据库。在Flink中使用FlinkCDC连接TiDB可以使用两种方式:使用FlinkCDC的Table API或使用FlinkCDC的Stream API。 使用FlinkCDC的Stream API连接TiDB的步骤如下: 1. 在TiDB中创建一个测试表并插入一些数据: ``` CREATE DATABASE test; USE test; CREATE TABLE user ( id INT PRIMARY KEY, name VARCHAR(20), age INT ); INSERT INTO user VALUES (1, 'Alice', 20), (2, 'Bob', 30); ``` 2. 在Flink中引入TiDB和FlinkCDC的相关依赖: ``` <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-jdbc_2.12</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-tidb_2.12</artifactId> <version>${flink.version}</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-cdc-connectors_2.12</artifactId> <version>${flink.version}</version> </dependency> ``` 3. 编写Flink程序,使用FlinkCDC的Stream API连接TiDB: ``` import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.functions.source.SourceFunction; import org.apache.flink.streaming.api.functions.source.SourceRecord; import org.apache.flink.streaming.api.functions.source.TimestampedSourceFunction; import org.apache.flink.streaming.connectors.tidb.TableId; import org.apache.flink.streaming.connectors.tidb.TiDBOptions; import org.apache.flink.streaming.connectors.tidb.TiDBSource; import org.apache.flink.streaming.connectors.tidb.TiDBTableSource; import org.apache.flink.types.Row; import java.util.Properties; public class TiDBStreamAPIExample { public static void main(String[] args) throws Exception { StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); Properties properties = new Properties(); properties.setProperty(TiDBOptions.TIDB_ADDRESS.key(), "localhost:4000"); properties.setProperty(TiDBOptions.TIDB_PASSWORD.key(), ""); properties.setProperty(TiDBOptions.TIDB_USER.key(), "root"); TiDBTableSource tableSource = TiDBSource .builder() .setDatabaseName("test") .setTableName("user") .setProperties(properties) .build(); env.addSource(tableSource).print(); env.execute("TiDB Stream API Example"); } } ``` 4. 运行程序,可以看到从TiDB中读取到的数据: ``` 1,Alice,20 2,Bob,30 ``` 以上就是使用FlinkCDC的Stream API连接TiDB的步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

九师兄

你的鼓励是我做大写作的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值