I am trying to calculate the average for input datastream (no windows) in Flink
I have used a mapper to change the stream from (key, value) to (key, value, 1)
Now I need to sum on both 2nd and 3rd field and divide them by each other.
Input data stream is from socket connection in form of 'KEY VALUE' like 'X 5'
public class AvgViews {
DataStream<Tuple2<String, Double>> AvgViewStream = dataStream
.map(new AvgViews.RowSplitter())
.keyBy(0)
//.???
public static class RowSplitter implements
MapFunction<String, Tuple3<String, Double, Integer>> {
public Tuple3<String, Double, Integer> map(String row)
throws Exception {
String[] fields = row.split(" ");
if (fields.length == 2) {
return new Tuple3<String, Double, Integer>(
fields[0],
Double.parseDouble(fields[1]),
1);
}
return null;
}
}
}