我已经查看了来自互联网的标签传播算法源代码,并且在自定义算法的一部分中使用了它。我使用的代码如下:
def run[VD, ED: ClassTag](graph: Graph[VD, ED], maxSteps: Int): Graph[VertexId, ED] = {
require(maxSteps > 0, s"Maximum of steps must be greater than 0, but got ${maxSteps}")
val lpaGraph: Graph[VD, ED] = graph.mapVertices { case (y,x) => x}
def sendMessage(e: EdgeTriplet[VertexId, ED]): Iterator[(VertexId, Map[VertexId, Long])] = {
Iterator((e.srcId, Map(e.dstAttr -> 1L)), (e.dstId, Map(e.srcAttr -> 1L)))
}
def mergeMessage(count1: Map[VertexId, Long], count2: Map[VertexId, Long])
: Map[VertexId, Long] = {
// Mimics the optimization of breakOut, not present in Scala 2.13, while working in 2.12
val map = mutable.Map[VertexId, Long]()
(count1.keySet ++ count2.keySet).map { i =>
val count1Val = count1.getOrElse(i, 0L)
val count2Val = count2.getOrElse(i, 0L)
map.put(i, count1Val + count2Val)
}
map
}
def vertexProgram(vid: VertexId, attr: Long, message: Map[VertexId, Long]): VertexId = {
if (message.isEmpty) attr else message.maxBy(_._2)._1
}
val initialMessage = Map[VertexId, Long]()
Pregel(lpaGraph, initialMessage, maxIterations = maxSteps)(
vprog = vertexProgram,
sendMsg = sendMessage,
mergeMsg = mergeMessage)
}
在原始源代码的开头,每个节点都获得与该代码相同的ID作为初始标签(属性):
val lpaGraph: Graph[VD, ED] = graph.mapVertices { case (y,x) => y}
但是在我的自定义算法中,我希望节点通过使用以下代码将其当前标签(属性)作为其初始标签:
val lpaGraph: Graph[VD, ED] = graph.mapVertices { case (y,x) => x}
但是当我使用上面的代码时,出现一些错误,例如vprog = vertexProgram
中的错误,错误是类型不匹配:
Error:(176, 17) type mismatch;
found : org.apache.spark.graphx.VertexId (which expands to) Long
required: VD
vprog = vertexProgram,
有人可以帮助我解决这个问题吗?我已经使用GraphLoader加载边缘列表以制作图形,而RDD如下所示:
(1,2)
(2,2)
(3,8)
...