简体   繁体   English

Flink DataStream 元素未更新

[英]Flink DataStream element not updating

I have a code like:我有一个像这样的代码:

DataStream<Sample> stream = ...

stream.map(x => {
    x.setVal(10);
    return x;
});

// Sample class looks like this:
class Sample {
    int val = 5;
    public void setVal(int val) {
        this.val = val;
    }
}

When I check this stream by printing, I noticed that x.setVal() is not working.当我通过打印检查这个 stream 时,我注意到 x.setVal() 不起作用。 x.val remains 5, it doesn't become 10. I tried also doing x.val = 10 , that didn't work too. x.val 仍然是 5,它不会变成 10。我也尝试过x.val = 10 ,但也没有用。

It works for the below code, Did you do env.execute ?它适用于下面的代码,你做了env.execute吗?

Note that if you don't call execute(), your application won't be run.请注意,如果您不调用 execute(),您的应用程序将不会运行。

https://nightlies.apache.org/flink/flink-docs-master/docs/learn-flink/datastream_api/#a-complete-example https://nightlies.apache.org/flink/flink-docs-master/docs/learn-flink/datastream_api/#a-complete-example

static class Sample {
    int val = 5;

    public void setVal(int val) {
        this.val = val;
    }
    public String toString(){
        return "My val is " + val;
    }
}

public static void main(String[] args) throws Exception {

    final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment();

    DataStream<Sample> mySamples = env.fromElements(new Sample());
    mySamples.map(sample -> {
                 sample.setVal(10);
                 return sample;
             })
             .print();

    env.execute();
}

Log:日志:

09:59:46.838 [Map -> Sink: Print to Std. Out (10/16)#0] DEBUG org.apache.flink.runtime.taskmanager.Task - Ensuring all FileSystem streams are closed for task Map -> Sink: Print to Std. Out (10/16)#0 (bf54e7d1b353295b15654e2448deccf3) [FINISHED]
09:59:46.838 [Map -> Sink: Print to Std. Out (9/16)#0] DEBUG org.apache.flink.runtime.io.network.partition.consumer.SingleInputGate - Map -> Sink: Print to Std. Out (9/16)#0 (7ed1412933822e68476429e2dcb945b4): Releasing SingleInputGate{owningTaskName='Map -> Sink: Print to Std. Out (9/16)#0 (7ed1412933822e68476429e2dcb945b4)', gateIndex=0}.
09:59:46.838 [Map -> Sink: Print to Std. Out (11/16)#0] DEBUG org.apache.flink.runtime.taskmanager.Task - Ensuring all FileSystem streams are closed for task Map -> Sink: Print to Std. Out (11/16)#0 (f051a157bfd41d38ad1d645543384003) [FINISHED]
09:59:46.838 [Map -> Sink: Print to Std. Out (9/16)#0] DEBUG org.apache.flink.runtime.taskmanager.Task - Ensuring all FileSystem streams are closed for task Map -> Sink: Print to Std. Out (9/16)#0 (7ed1412933822e68476429e2dcb945b4) [FINISHED]

7> My val is 10

09:59:46.838 [Source: Collection Source (1/1)#0] DEBUG org.apache.flink.runtime.io.network.partition.PipelinedSubpartition - Source: Collection Source (1/1)#0 (915eb1a21636f7a00f9b7b37d82225c3): Finished PipelinedSubpartition#14 [number of buffers: 4 (21 bytes), number of buffers in backlog: 0, finished? true, read view? true].
09:59:46.838 [Map -> Sink: Print to Std. Out (14/16)#0] DEBUG org.apache.flink.runtime.io.network.partition.ResultPartition - PipelinedResultPartition 85e04b269c1186f0664dc9ee99034cf3#0@915eb1a21636f7a00f9b7b37d82225c3 [PIPELINED_BOUNDED, 16 subpartitions, 4 pending consumptions]: Received consumed notification for subpartition 13.
09:59:46.838 [Map -> Sink: Print to Std. Out (15/16)#0] DEBUG org.apache.flink.runtime.io.network.partition.ResultPartition - PipelinedResultPartition 85e04b269c1186f0664dc9ee99034cf3#0@915eb1a21636f7a00f9b7b37d82225c3 [PIPELINED_BOUNDED, 16 subpartitions, 3 pending consumptions]: Received consumed notification for subpartition 14.
09:59:46.838 [Map -> Sink: Print to Std. Out (14/16)#0] DEBUG org.apache.flink.streaming.runtime.tasks.StreamTask - Finished task Map -> Sink: Print to Std. Out (14/16)#0
09:59:46.838 [Map -> Sink: Print to Std. Out (15/16)#0] DEBUG org.apache.flink.streaming.runtime.tasks.StreamTask - Finished task Map -> Sink: Print to Std. Out (15/16)#0
09:59:46.838 [Source: Collection Source (1/1)#0] DEBUG org.apache.flink.runtime.io.network.partition.PipelinedSubpartition - Source: Collection Source (1/1)#0 (915eb1a21636f7a00f9b7b37d82225c3): Finished PipelinedSubpartition#15 [number of buffers: 4 (21 bytes), number of buffers in backlog: 0, finished? true, read view? true].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM