In flink, flatMap can also emit one record. It seems flatMap can replace map. Can somebody tell me the differrence in this situation? Thank you.
In flink, flatMap can also emit one record. It seems flatMap can replace map. Can somebody tell me the differrence in this situation? Thank you.
If you are thinking that map is superfluous, you are correct. Flatmap can do everything map can do, and more.
Nevertheless, I do use map in situations where there is a strict one-to-one correspondence between input and output. This serves as a clear indication to the reader that, for example, there are no situations where errors or invalid input would cause the operation to fail to produce an output record.
The main difference between map and flatMap is the return type.
Both methods work on DataStream and DataSet objects and executed for each element in the stream or the set.
However, the map method returns exactly one element, whereas the flatMap returns a collection (which can hold none, one, or more elements).
map method receives an argument of the interface MapFunction with the method O out map(I elem). The input is an element, the output is one object. flatMap method receives an argument of the interface FlatMapFunction with the method void flatMap(I elem, Collector<O> out). The input is an element, the output is a collection of objects. Using map() also makes it easy to use lambdas. For example, to convert a Tuple2<Integer, String> to just the String, you can do.
.map(value -> value.f1)