原因:java.io.IOException:无法序列化Tuple2类型的默认值

时间:2018-07-27 03:44:59

标签: apache-flink

我运行官方演示时不起作用

代码网址https://ci.apache.org/projects/flink/flink-docs-release-1.5/quickstart/run_example_quickstart.html#writing-a-flink-program

package wikiedits;

import org.apache.flink.api.common.functions.FoldFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditEvent;
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditsSource;

public class WikipediaAnalysis {

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

    StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();

    DataStream<WikipediaEditEvent> edits = see.addSource(new WikipediaEditsSource());

    KeyedStream<WikipediaEditEvent, String> keyedEdits = edits
      .keyBy(new KeySelector<WikipediaEditEvent, String>() {
        @Override
        public String getKey(WikipediaEditEvent event) {
          return event.getUser();
        }
      });

    DataStream<Tuple2<String, Long>> result = keyedEdits
      .timeWindow(Time.seconds(5))
      .fold(new Tuple2<>("", 0L), new FoldFunction<WikipediaEditEvent, Tuple2<String, Long>>() {
        @Override
        public Tuple2<String, Long> fold(Tuple2<String, Long> acc, WikipediaEditEvent event) {
          acc.f0 = event.getUser();
          acc.f1 += event.getByteDiff();
          return acc;
        }
      });

    result.print();

    see.execute();
  }
}

运行命令:

$ mvn exec:java -Dexec.mainClass=wikiedits.WikipediaAnalysis

运行结果:

Exception in thread "main" org.apache.flink.streaming.runtime.tasks.StreamTaskException: Cannot serialize operator object class org.apache.flink.streaming.runtime.operators.windowing.WindowOperator.
    at org.apache.flink.streaming.api.graph.StreamConfig.setStreamOperator(StreamConfig.java:187)
    at org.apache.flink.streaming.api.graph.StreamingJobGraphGenerator.setVertexConfig(StreamingJobGraphGenerator.java:322)
    at org.apache.flink.streaming.api.graph.StreamingJobGraphGenerator.createChain(StreamingJobGraphGenerator.java:216)
    at org.apache.flink.streaming.api.graph.StreamingJobGraphGenerator.createChain(StreamingJobGraphGenerator.java:207)
    at org.apache.flink.streaming.api.graph.StreamingJobGraphGenerator.setChaining(StreamingJobGraphGenerator.java:177)
    at org.apache.flink.streaming.api.graph.StreamingJobGraphGenerator.createJobGraph(StreamingJobGraphGenerator.java:126)
    at org.apache.flink.streaming.api.graph.StreamGraph.getJobGraph(StreamGraph.java:580)
    at org.apache.flink.streaming.api.environment.LocalStreamEnvironment.execute(LocalStreamEnvironment.java:89)
    at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.execute(StreamExecutionEnvironment.java:1170)
    at wikiedits.WikipediaAnalysis.main(WikipediaAnalysis.java:42)
Caused by: java.io.IOException: Unable to serialize default value of type Tuple2.
    at org.apache.flink.api.common.state.StateDescriptor.writeObject(StateDescriptor.java:334)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1128)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    at org.apache.flink.util.InstantiationUtil.serializeObject(InstantiationUtil.java:512)
    at org.apache.flink.util.InstantiationUtil.writeObjectToConfig(InstantiationUtil.java:442)
    at org.apache.flink.streaming.api.graph.StreamConfig.setStreamOperator(StreamConfig.java:184)
    ... 9 more
Caused by: java.lang.NullPointerException
    at org.apache.flink.api.common.state.StateDescriptor.writeObject(StateDescriptor.java:326)
    ... 25 more

1 个答案:

答案 0 :(得分:0)

通过删除flink依赖项提供的作用域,我能够从命令行运行此示例。这四个依赖项需要与文档中显示的内容完全匹配(显然,在这一点上需要更加清楚):

#ifndef HEADER_H
#define HEADER_H

#include <thread>

class A
{
private:
    int someint;
public:
    void Run();
};

class B
{
public:
    void Run();
};

class ThreadManager
{
private:
    A obj_class_A_;
    B obj_class_B_;

    std::thread obj_thread_A_;
    std::thread obj_thread_B_;

public:
    ThreadManager();
    ~ThreadManager();
};

#endif // HEADER_H