You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@flink.apache.org by 陈梓立 <wa...@gmail.com> on 2018/05/04 21:36:45 UTC

Why FoldFunction deprecated?

I just write a code snip like

```
.fold(new Tuple2<>("", 0L), new FoldFunction<WikipediaEditEvent,
Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> fold(Tuple2<String, Long>
acc, WikipediaEditEvent event) {
                        acc.f0 = event.getUser();
                        acc.f1 += event.getByteDiff();
                        return acc;
                    }
                });
```

and replace it using `aggregate()`

```
.aggregate(new AggregateFunction<WikipediaEditEvent, Tuple2<String, Long>,
Tuple2<String,Long>>() {
                    @Override
                    public Tuple2<String, Long> createAccumulator() {
                        return new Tuple2<>("", 0L);
                    }

                    @Override
                    public Tuple2<String, Long> add(WikipediaEditEvent
event, Tuple2<String, Long> acc) {
                        return new Tuple2<>(event.getUser(), acc.f1 +
event.getByteDiff());
                    }

                    @Override
                    public Tuple2<String, Long> getResult(Tuple2<String,
Long> acc) {
                        return acc;
                    }

                    @Override
                    public Tuple2<String, Long> merge(Tuple2<String, Long>
a, Tuple2<String, Long> b) {
                        return new Tuple2<>(a.f0, a.f1 + b.f1);
                    }
                });
```

It seems I have to write much more code using `aggregate()`

Is there something I miss so that write so much code? Or say, maybe
`aggregate()` is expressive, but why `fold()` deprecated? Since `fold` is a
general concept people can understand.

Re: Why FoldFunction deprecated?

Posted by Fabian Hueske <fh...@gmail.com>.
Hi,

FoldFunction was deprecated because it doesn't support partial aggregation.
AggregateFunction is much more expressive, however requires a bit more
implementation effort.
In favor of a concise API, FoldFunction was deprecated because it doesn't
offer more functionality than AggregateFunction.

You can implement your use case also easily using a MapFunction before the
window, that maps WikipediaEditEvent to Tuple2<String, Long> and a
ReduceFunction in the window that sums the length field.

Best, Fabian

2018-05-04 23:36 GMT+02:00 陈梓立 <wa...@gmail.com>:

> I just write a code snip like
>
> ```
> .fold(new Tuple2<>("", 0L), new FoldFunction<WikipediaEditEvent,
> Tuple2<String, Long>>() {
>                     @Override
>                     public Tuple2<String, Long> fold(Tuple2<String, Long>
> acc, WikipediaEditEvent event) {
>                         acc.f0 = event.getUser();
>                         acc.f1 += event.getByteDiff();
>                         return acc;
>                     }
>                 });
> ```
>
> and replace it using `aggregate()`
>
> ```
> .aggregate(new AggregateFunction<WikipediaEditEvent, Tuple2<String,
> Long>, Tuple2<String,Long>>() {
>                     @Override
>                     public Tuple2<String, Long> createAccumulator() {
>                         return new Tuple2<>("", 0L);
>                     }
>
>                     @Override
>                     public Tuple2<String, Long> add(WikipediaEditEvent
> event, Tuple2<String, Long> acc) {
>                         return new Tuple2<>(event.getUser(), acc.f1 +
> event.getByteDiff());
>                     }
>
>                     @Override
>                     public Tuple2<String, Long> getResult(Tuple2<String,
> Long> acc) {
>                         return acc;
>                     }
>
>                     @Override
>                     public Tuple2<String, Long> merge(Tuple2<String, Long>
> a, Tuple2<String, Long> b) {
>                         return new Tuple2<>(a.f0, a.f1 + b.f1);
>                     }
>                 });
> ```
>
> It seems I have to write much more code using `aggregate()`
>
> Is there something I miss so that write so much code? Or say, maybe
> `aggregate()` is expressive, but why `fold()` deprecated? Since `fold` is a
> general concept people can understand.
>