I want to create a DataStream inside a flatMap function. However I when I run dataStream.print()
, it shows nothing. Here is my code:
public class test {
static StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
public static void main(String[] args) throws Exception {
String s = "test message";
DataStream<String> data = env.fromElements(s);
data.flatMap(new customFlatMap()).print();
env.execute();
}
private static class customFlatMap extends RichFlatMapFunction<String, String> {
@Override
public void flatMap(String s, Collector<String> collector) {
DataStream<String> newStream = env.fromElements(s);
System.out.println("-----");
newStream.print();
System.out.println("-----");
collector.collect(s);
}
}
}
When I run my code, it prints the following result:
-----
-----
8> test message
Nothing shows between two dashes.
Can someone help me explain this? Thank you in advance!
You need to create a side output, which can't be done if you're using a FlatMap function.