Scala GraphX:没有缓存的输出错误()

Scala GraphX:没有缓存的输出错误(),scala,apache-spark,Scala,Apache Spark,我正在做以下工作: var count = 0 while(count > 0){ val messages = graph.vertices.flatMap{ // Create messages for other nodes } // Cache which is critical for the correct execution count.cache() count = messages.count() val

我正在做以下工作:

var count = 0
while(count > 0){
    val messages = graph.vertices.flatMap{
        // Create messages for other nodes
    }

    // Cache which is critical for the correct execution
    count.cache()
    count = messages.count()

    val msgType1 = messages.filter()
    val msgType2 = messages.filter()


    println(count)
    //Should be exactly messages.count()
    println(msgType1.count() + msgType2.count())
    println("---")
}
如果我像这样执行它,那么输出是:

8
6 2
---
11
3 8
---
0
0 0
---
这些加起来正好是邮件数量

如果在flatMap操作后删除count.cache,则在对消息进行计数后,对消息的过滤是错误的。看起来计数清除了信息之类的东西

然后输出为:

8
0 0
---
0
0 0
---
为什么会这样?我的程序只有在使用缓存操作时才能工作,或者它也可以在不缓存消息的情况下工作吗?

我的问题是,如果在一次循环迭代中调用一次flatmap,那么输出是正确的

如果在可能发生的一次迭代中调用两次,如果必须重新计算消息,则第一次输出是正确的,而下面的输出是不正确的,因为flatmap中的我的操作只能在每个节点和节点上执行一次

因此,如果调用cache,flatmap只执行一次。在没有缓存的情况下,每次计数操作都会调用它,因此第一个是正确的,接下来的两个是错误的