![](/img/trans.png)
[英]How to close shared singleton connection between cores in Spark Executor
[英]Creating singleton object in Spark for each executor
我在这里描述了一个非常类似的问题: How to perform an operation on each executor once in spark我遵循了第一个答案中的第一种方法,但仍然遇到了序列化问题。
我想要做的是,我有像 (sourceVertex, targetVertex) 元组这样的查询,并将它们发送给执行程序,执行程序将返回给我一条最短路径。 为此,我正在使用 jgrapht。
当我像这样实施时
class ShortestPath(graph: SimpleDirectedWeightedGraph[Node, DefaultWeightedEdge],
bc: Broadcast[SimpleDirectedWeightedGraph[Node, DefaultWeightedEdge]]) extends Serializable {
def calculateShortestPath(vertexRDD: RDD[Node]) = {
val result = vertexRDD.map(vertex => {
val dijkstraShortestPath: DijkstraShortestPath[Node, DefaultWeightedEdge]
= new DijkstraShortestPath[Node, DefaultWeightedEdge](bc.value)
val distanceIn = dijkstraShortestPath.getPath(vertex, Node(4, 1, true)).getWeight()
distanceIn
})
result.collect().foreach(println(_))
}
}
object ShortestPath {
def apply(graph: SimpleDirectedWeightedGraph[Node, DefaultWeightedEdge],
bc: Broadcast[SimpleDirectedWeightedGraph[Node, DefaultWeightedEdge]]): ShortestPath = new ShortestPath(graph, bc)
}
一切正常但问题是我想我正在为每个任务创建dijkstraShortestPath
对象,对吗?
我的目标是为每个执行程序创建此对象,并将其用于该执行程序上的每个任务。
我给出的链接说用惰性值创建一个对象,在这里实例化你的想法,然后使用它 RDD 映射函数。 我实现了这样的解决方案:
object Dij {
lazy val dijsktra = {
val graph = GraphCreator.createGraph()
val dijkstraShortestPath: DijkstraShortestPath[Node, DefaultWeightedEdge] = new DijkstraShortestPath[Node, DefaultWeightedEdge](graph)
dijkstraShortestPath
}
}
并在 ShortestPath 类中使用
val result = vertexRDD.map(vertex => {
val dijkstraShortestPath = Dij.dijsktra
val distanceIn = dijkstraShortestPath.getPath(vertex, Node(4, 1, true)).getWeight()
dijkstraShortestPath
})
result.collect().foreach(println(_))
但后来我收到序列化错误谢谢说- object not serializable (class: org.jgrapht.alg.shortestpath.DijkstraShortestPath, value: org.jgrapht.alg.shortestpath.DijkstraShortestPath@2cb8e13b)
这是正确的,当我在那里查看实现时是不可序列化的。
另一个问题是如果它不是 Serializable 那么我的第一个实现是如何工作的?
我认为您的第二个代码段中存在意外错误。
赋予地图的第一个函数返回一个权重(大概是一个Double
?),第二个函数返回一个不可序列化的DijkstraShortestPath
。 这解释了为什么这两个片段的行为不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.