Apache Flink:执行扩展远程集群上RichFlatMapFunction的程序会导致错误

时间:2015-12-11 13:13:11

标签: java apache-flink

我在Apache Flink中有以下代码。它在本地集群中正常工作,而在远程集群上运行它会在包含命令" stack.push(recordPair);"。

的行中生成NullPointerException错误。

有谁知道,是什么原因?

本地和远程群集的输入数据集相同。

public static class TC extends RichFlatMapFunction<Tuple2<Integer, Integer>, Tuple2<Integer, Integer>> {
            private static TreeSet<Tuple2<Integer, Integer>> treeSet_duplicate_pair  ;
            private  static HashMap< Integer, Set<Integer>> clusters_duplicate_map ;
            private  static  Stack<Tuple2< Integer,Integer>> stack ;
            public TC(List<Tuple2<Integer, Integer>> duplicatsPairs) {
        ...
                stack = new Stack<Tuple2< Integer,Integer>>();
            }
            @Override
            public void flatMap(Tuple2<Integer, Integer> recordPair, Collector<Tuple2<Integer, Integer>> out) throws Exception {
    if (recordPair!= null)
    {
                stack.push(recordPair);
    ...
    }
    }

1 个答案:

答案 0 :(得分:2)

问题是您在stack类的构造函数中初始化TC变量。这仅为运行客户端程序的JVM初始化静态变量。对于本地执行,这是有效的,因为Flink作业在同一JVM中执行。

在群集上运行时,TC将被序列化并传送到群集节点。在那里,实例的反序列化不会再次调用构造函数来初始化stack。为了使这项工作,您应该将初始化逻辑移动到open的{​​{1}}方法或使用静态初始化器。但请注意,在RichFlatMapFunction上运行的所有运算符将共享同一个TaskManager实例,因为它是一个类变量。

stack
相关问题