I have a Flink job, and one of the statefull operators needs to keep into the state a class that contains a HashMap as attribute, because this hasMap keeps different affinities for a user, example:
public class Affinity {
public String id;
public String colorTriggered;
public Map<String,Integer> affinities;
/*this object keeps the affinity for a user to a different colors for example:
affinities.put(green, 5);
affinities.put(blue, 9);
affinities.put(white, 2);
to calculate then what is the color's affinity of this user, in this case the answer will be blue
*/
}
This hashMap is used to track those affinities and in certain moment ask for the color's affinity of a user and get the key of the highest affinity value which will be blue that the value 9.
As hashMaps are not part of Flink serialization I will need to include implement Serializable
to my class.
Is that a bad idea or there is a better way to do this and keep the object into the states?
In a full example more or less what I need to do but not sure if using HashMap into a Flink operator and into states is a good idea:
public class AffinityFlatMapFunction extends RichFlatMapFunction<Event, Affinity> implements MapOperations {
@Override
public void flatMap(Event event, Collector<Affinity> collector) throws Exception {
Affinity previous = state.value();
if(previous.hashMap.contains(event.color)){
previous.hashMap.replace(event.color, value + 1);
}else previous.hashMap.put(event.color, 1);
/*something like this*/
String match = previous.hashMap.stream.filter(x ->
x.getKey().contains(event.color)).max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey).orElse("empty");
if(!match.equals(previous.colorTriggered){
previous.colorTriggered = match;
state.update(previous);
collector.collect(previous);
}
}
}
Kind regards!