You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-zh@flink.apache.org by 刘仲诺 <23...@qq.com.INVALID> on 2024/08/29 11:32:45 UTC

回复:Re:回复:flink对象重用问题询问

感谢回复,是不是可以这样理解,所有使用out进行输出的地方都可以进行对象重用,因为会发生拷贝,而比如AggregateFunction当中的使用return进行输出的函数就不能使用这种对象重用



---原始邮件---
发件人: "Xuyang"<xyzhong131@163.com&gt;
发送时间: 2024年8月29日(周四) 下午3:18
收件人: "user-zh"<user-zh@flink.apache.org&gt;;
主题: Re:回复:flink对象重用问题询问


Hi,

应该是可以的。




--

    Best!
    Xuyang





在 2024-08-29 15:00:54,"刘仲诺" <2313678365@qq.com.INVALID&gt; 写道:
&gt;您好,我目前使用的是DataStream API,代码如下:
&gt;public class BytesProcessor extends ProcessWindowFunction<Tuple2&gt;&nbsp; &nbsp; private final BytesResult bytesResult = new BytesResult();
&gt;&nbsp; &nbsp; @Override
&gt;&nbsp; &nbsp; public void process(String key,
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessWindowFunction<Tuple2&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterable<Tuple2&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collector&gt;&nbsp; &nbsp; &nbsp; &nbsp; Tuple2&gt;//&nbsp; &nbsp; &nbsp; &nbsp; BytesResult bytesResult = new BytesResult();
&gt;&nbsp; &nbsp; &nbsp; &nbsp; bytesResult.setChannelId(key);
&gt;&nbsp; &nbsp; &nbsp; &nbsp; bytesResult.setLastHopRecvBytes(result.f0);
&gt;&nbsp; &nbsp; &nbsp; &nbsp; bytesResult.setNextHopSendBytes(result.f1);
&gt;&nbsp; &nbsp; &nbsp; &nbsp; bytesResult.setCurrentTime(context.window().getStart());
&gt;&nbsp; &nbsp; &nbsp; &nbsp; if (!Objects.equals(result.f0, result.f1)) {
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.collect(bytesResult);
&gt;&nbsp; &nbsp; &nbsp; &nbsp; }
&gt;&nbsp; &nbsp; }
&gt;}
&gt;
&gt;我在这里用算子实例的bytesResult变量代替了每次输出新建的bytesResult,这样的做法是否安全呢
&gt;
&gt;
&gt;
&gt;
&gt;------------------&nbsp;原始邮件&nbsp;------------------
&gt;发件人:                                                                                                                        "user-zh"                                                                                    <xyzhong131@163.com&gt;;
&gt;发送时间:&nbsp;2024年8月29日(星期四) 下午2:56
&gt;收件人:&nbsp;"user-zh"<user-zh@flink.apache.org&gt;;
&gt;
&gt;主题:&nbsp;Re:flink对象重用问题询问
&gt;
&gt;
&gt;
&gt;Hi,
&gt;
&gt;在算子output时,是可以复用row的,在flink sql所转化的operator中,就有很多类似的做法,具体可以参考[1][2]
&gt;
&gt;
&gt;
&gt;
&gt;[1] https://github.com/apache/flink/blob/576ec2b9361a3f8d58fb22b998b0ca7c3c8cf10e/flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/join/stream/StreamingJoinOperator.java#L45
&gt;
&gt;[2] https://github.com/apache/flink/blob/576ec2b9361a3f8d58fb22b998b0ca7c3c8cf10e/flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/aggregate/GroupAggFunction.java#L66
&gt;
&gt;
&gt;
&gt;
&gt;--
&gt;
&gt;&nbsp;&nbsp;&nbsp; Best!
&gt;&nbsp;&nbsp;&nbsp; Xuyang
&gt;
&gt;
&gt;
&gt;
&gt;
&gt;在 2024-08-29 12:42:28,"刘仲诺" <2313678365@qq.com.INVALID&gt; 写道:
&gt;&gt;您好,目前我正在开发flink流计算程序,请问flink在算子函数中是否允许对象重用呢,比如我在构造输出记录时,不是每次都新建一条记录,而是只新建一个记录对象作为算子函数实例的成员,在构造输出记录时只更改这个对象的属性然后进行输出,请问这样的做法在flink中是安全的吗?我想进行这种操作的主要目的是减少对象创建