1

我有一个问题,类型 DefaultMutableTreeNode 变量的值一旦在 Spark mapToPair() 函数中使用就会变为默认值。这是我的代码:

public class CA implements Serializable{
    private final JavaRDD<String> input;
    private final List<IB> bList;
    public boolean FuncWithSpark(){
    /* 
    !!!at this point, bList.get(0).getD().getRoot() return a valid tree node
    */
    JavaRDD<Boolean> counters = input.mapToPair(new PairFunction<String, String, List<String>>() {
          @Override
          public Tuple2<String, List<String>> call(String s) throws Exception {
              /* 
              !!!at this point, bList.get(0).getD().getRoot() return an uninitialized tree node with default values
              */
              ...
          }
      }
    }

    public CA(JavaRDD<String> input, List<IB> bList) {
        this.input = input;
        this.bList = bList;
  }
}

接口 IB、ID、CB 和 CD 类定义如下:

public interface IB {
  ...
}
public interface ID {
  ...
}

public class CB implements IB, Serializable{
    private final ID d;
    public ID getD(){
        return this.d;
    }
}
public class CD implements ID, Serializable{
    private DefaultMutableTreeNode rootNode;

    public DefaultMutableTreeNode getRoot(){
      return this.rootNode;
    }
}

问题是,CA.FuncWithSpark() 中 DefaultMutableTreeNode 类型的变量发生了什么?是因为 Spark 转换,还是 DefaultMutableTreeNode 的成员变量受到保护而没有访问者?请给我一个解决这个问题的方向。感谢您提前提供任何帮助!

4

1 回答 1

0

由于我是 Apache Spark 的新手,而且这是我第一次使用 DefaultMutableTreeNode 类,我无法解释根本原因,但我找到了一种让我的代码工作的方法。DefaultMutableTreeNode的文档提到This is not a thread safe class,这让我想到在 Spark 中,将线程不安全类型的变量从驱动程序传递给执行程序可能无法正确传递值。

但是,我的项目需要像树节点这样的数据结构,所以我在stackoverflow上找到了这个通用树节点实现来替换 DefaultMutableTreeNode。现在我的代码运行良好。

于 2016-09-29T22:42:40.023 回答