[英]Creating singleton object in Spark for each executor
I've quite a similar problem described here: How to perform one operation on each executor once in spark I followed the first approach in the first answer but still got a Serialization problem.我在这里描述了一个非常类似的问题: How to perform an operation on each executor once in spark我遵循了第一个答案中的第一种方法,但仍然遇到了序列化问题。
What I want to do is, I have queries like a tuple of (sourceVertex, targetVertex) and send these to executors and executors will return to me a shortest path.我想要做的是,我有像 (sourceVertex, targetVertex) 元组这样的查询,并将它们发送给执行程序,执行程序将返回给我一条最短路径。 For this I'm using jgrapht.
为此,我正在使用 jgrapht。
When I implement like this当我像这样实施时
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)
}
Everything is okay But the problem is I think I'm creating dijkstraShortestPath
object for each task, am I right?一切正常但问题是我想我正在为每个任务创建
dijkstraShortestPath
对象,对吗?
My aim is to create this object for each executor and use it for each task on that executor.我的目标是为每个执行程序创建此对象,并将其用于该执行程序上的每个任务。
The link that I gave says create a Object with lazy val, instantiate your think here then use it RDD map function.我给出的链接说用惰性值创建一个对象,在这里实例化你的想法,然后使用它 RDD 映射函数。 I implement that solution like this:
我实现了这样的解决方案:
object Dij {
lazy val dijsktra = {
val graph = GraphCreator.createGraph()
val dijkstraShortestPath: DijkstraShortestPath[Node, DefaultWeightedEdge] = new DijkstraShortestPath[Node, DefaultWeightedEdge](graph)
dijkstraShortestPath
}
}
and used in ShortestPath class并在 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(_))
but then I'm getting serialization error thank says - object not serializable (class: org.jgrapht.alg.shortestpath.DijkstraShortestPath, value: org.jgrapht.alg.shortestpath.DijkstraShortestPath@2cb8e13b)
which is right, when i look implementation there is no Serializable.但后来我收到序列化错误谢谢说
- object not serializable (class: org.jgrapht.alg.shortestpath.DijkstraShortestPath, value: org.jgrapht.alg.shortestpath.DijkstraShortestPath@2cb8e13b)
这是正确的,当我在那里查看实现时是不可序列化的。
And another question is if it is not Serializable then how my first implementation worked?另一个问题是如果它不是 Serializable 那么我的第一个实现是如何工作的?
I think there is an unintended error in your second snippet.我认为您的第二个代码段中存在意外错误。
The first function given to a map returns a weight (presumably a Double
?) the second returns a DijkstraShortestPath
which is not serializable.赋予地图的第一个函数返回一个权重(大概是一个
Double
?),第二个函数返回一个不可序列化的DijkstraShortestPath
。 This explains why the two snippets behave differently.这解释了为什么这两个片段的行为不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.