Flink getResult()的聚合功能可以更改累加器值?

发布于 2025-01-23 12:53:38 字数 758 浏览 2 评论 0原文

getResult()函数可以更改累加器的值吗?在累积时仍会有效应值。 代码在下面:

public class WeightedAverage implements AggregateFunction<Datum, AverageAccumulator, Double> {

 public AverageAccumulator createAccumulator() {
     return new AverageAccumulator();
 }

 public AverageAccumulator merge(AverageAccumulator a, AverageAccumulator b) {
     a.count += b.count;
     a.sum += b.sum;
     return a;
 }

 public AverageAccumulator add(Datum value, AverageAccumulator acc) {
     acc.count += value.getWeight();
     acc.sum += value.getValue();
     return acc;
 }
 
 public Double getResult(AverageAccumulator acc) {
     int result = acc.sum / (double) acc.count;
     acc.count = 0; //here
     acc.sum = 0; //here
     return result;
 }

}

Can the getResult() function change the value of the accumulator?It still takes effect value when accumulating.
The code is in below:

public class WeightedAverage implements AggregateFunction<Datum, AverageAccumulator, Double> {

 public AverageAccumulator createAccumulator() {
     return new AverageAccumulator();
 }

 public AverageAccumulator merge(AverageAccumulator a, AverageAccumulator b) {
     a.count += b.count;
     a.sum += b.sum;
     return a;
 }

 public AverageAccumulator add(Datum value, AverageAccumulator acc) {
     acc.count += value.getWeight();
     acc.sum += value.getValue();
     return acc;
 }
 
 public Double getResult(AverageAccumulator acc) {
     int result = acc.sum / (double) acc.count;
     acc.count = 0; //here
     acc.sum = 0; //here
     return result;
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2025-01-30 12:53:38

不,您不能在getResult期间修改累加器。

Flink将根据需要创建新的蓄能器(例如,对于每个新窗口)。

No, you cannot modify the accumulator during getResult.

Flink will create new accumulators as necessary (e.g., for each new window).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文