Search code examples
javascalaapache-flinkfinalaccumulator

How to modify a value to Tuple2 in Java


I am using a accumulator within a fold function. I would like to change the value of the accumulator.

My function looks something like this:

public Tuple2<String, Long> fold(Tuple2<String, Long> acc, eventClass event) 
{
    acc._1 = event.getUser();
    acc._2 += event.getOtherThing();
    return acc
}

To me this should be working, because all I am doing is change the values of the accumulator. However what I get is Cannot assign value to final variable _1. Same for _2. Why are these properties of acc final? How can I assign values to them?

quick edit: Wat I could to is rust return a new Tuple instead, but this is not a nice solution in my opinion return new Tuple2<String, Long>(event.getUser(), acc._2 + event.getOtherThing());

solution for flink framework: Use the Tuple2 of defined in flink. Import it using

import org.apache.flink.api.java.tuple.Tuple2;

and then use it with

acc.f0 = event.getUser();
acc.f1 += event.getByteDiff();
return acc;

Solution

  • I don't know which Tuple2 you are working with. How about return a new object:

    Tuple2<String, Long> tuple = new Tuple2<String, Long>();
    tuple._1 = event.getUser();
    tuple._2 = event.getOtherThing() + acc._2;
    return tuple;