为什么数据流不打印转换?

时间:2019-01-19 06:32:28

标签: java apache-flink

List<Integer> lst = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
                .collect(Collectors.toList());
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<Integer> dataStream = env.fromCollection(lst);

dataStream.flatMap(new FlatMapFunction<Integer, Integer>() {

    @Override
    public void flatMap(Integer integer, Collector<Integer> collector) throws Exception {
        collector.collect(new Integer(integer.intValue() + 1));
    }
});

dataStream.print();
env.execute("Executing list");

这将结束打印:

10> 4
9> 3
8> 2
12> 6
11> 5
4> 10
1> 7
2> 8
3> 9
7> 1

我假设我在FlatMap函数的某个地方或打印中的某个地方犯了错误。我试图对其进行映射,以使每个元素都因为增加了1。

2 个答案:

答案 0 :(得分:0)

dataStream.flatMap(...)返回转换后的流,但不修改原始的dataStream。您需要将其重写为

dataStream.flatMap(...).print();

或为

DataStream<Integer> transformed = dataStream.flatMap(...);
transformed.print();

答案 1 :(得分:0)

dataStream.flatMap(...)。print()

不是

dataStream.flatMap(...) dataStream.print()