我正在运行一个用 Scala 2.9.3 编写的 spark 应用程序。下面是network
创建节点树的函数。每个节点都有一组唯一的邻居,它们是该特定节点的子节点。我面临的问题是对象是循环current
内的不同对象(从其不同的地址可以看出) 。for
如何防止这种情况并让for
循环在我在外面声明的同一个对象上执行?
def network (root: Node) : Tree =
{
var tree = new Tree(root)
var queue = ListBuffer[Node](root)
while (!queue.isEmpty && queue(0).level<maxlen)
{
var current: Node = queue.remove(0)
println(">>>>>>>>>>>>>>>>>> Current1: "+current)
var neigh = findNeighbor(current.userID)
for (n <- neigh)
{
if(tree.search(n._1) == null)
{
var c = new Node(n._1, current.level+1, n._2, n._3)
current.addChild(c)
println(">>>>>>>>>>>>>>>>>> Current2: "+current)
}
}
println(">>>>>>>>>>>>>>>>>> Current3: "+current)
queue ++= current.neighbors
}
return tree
}
下面是代码的输出。检查current
由 Current1、Current2 和 Current3 表示的 3 个位置的值。我们观察到 Current1 == Current3
[vijaygkd@is-joshbloom-hadoop network]$ sbt run
Loading /usr/local/sbt/bin/sbt-launch-lib.bash
[info] Set current project to Network (in build file:/usr/local/spark/test/vijay/network/)
[info] Compiling 1 Scala source to /usr/local/spark/test/vijay/network/target/scala-2.9.3/classes...
[info] Running Network
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
>>>>>>>>>>>>>>>>>> Current1: Node@76ab909a
13/10/13 14:23:50 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
13/10/13 14:23:50 WARN snappy.LoadSnappy: Snappy native library not loaded
13/10/13 14:23:50 INFO mapred.FileInputFormat: Total input paths to process : 1
>>>>>>>>>>>>>>>>>> Current2: Node@4f9e2851
>>>>>>>>>>>>>>>>>> Current2: Node@4f9e2851
>>>>>>>>>>>>>>>>>> Current2: Node@4f9e2851
>>>>>>>>>>>>>>>>>> Current2: Node@4f9e2851
>>>>>>>>>>>>>>>>>> Current3: Node@76ab909a
[success] Total time: 11 s, completed Oct 13, 2013 2:23:51 PM
附加信息:findNeighbor
返回一个包含 node 的邻居元组的 RDD userID
。
tree.search
函数检查树以查找树中是否n
已经存在。仅当节点不存在于树中时才将其添加为子节点。所有功能都按预期工作。