![](/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.