Spark 示例程序运行很慢
Spark example program runs very slow
我尝试使用 Spark 来解决简单的图形问题。我在 Spark 源文件夹中找到了一个示例程序:transitive_closure.py,它计算不超过 200 个边和顶点的图形中的传递闭包。但是在我自己的笔记本电脑上,它运行了 10 多分钟并且没有终止。我使用的命令行是:spark-submit transitive_closure.py。
我想知道为什么即使计算这么小的传递闭包结果,spark 也这么慢?这是一个常见的情况吗?有什么我想念的配置吗?
程序如下所示,可以在他们网站的spark install文件夹中找到。
from __future__ import print_function
import sys
from random import Random
from pyspark import SparkContext
numEdges = 200
numVertices = 100
rand = Random(42)
def generateGraph():
edges = set()
while len(edges) < numEdges:
src = rand.randrange(0, numEdges)
dst = rand.randrange(0, numEdges)
if src != dst:
edges.add((src, dst))
return edges
if __name__ == "__main__":
"""
Usage: transitive_closure [partitions]
"""
sc = SparkContext(appName="PythonTransitiveClosure")
partitions = int(sys.argv[1]) if len(sys.argv) > 1 else 2
tc = sc.parallelize(generateGraph(), partitions).cache()
# Linear transitive closure: each round grows paths by one edge,
# by joining the graph's edges with the already-discovered paths.
# e.g. join the path (y, z) from the TC with the edge (x, y) from
# the graph to obtain the path (x, z).
# Because join() joins on keys, the edges are stored in reversed order.
edges = tc.map(lambda x_y: (x_y[1], x_y[0]))
oldCount = 0
nextCount = tc.count()
while True:
oldCount = nextCount
# Perform the join, obtaining an RDD of (y, (z, x)) pairs,
# then project the result to obtain the new (x, z) paths.
new_edges = tc.join(edges).map(lambda __a_b: (__a_b[1][1], __a_b[1][0]))
tc = tc.union(new_edges).distinct().cache()
nextCount = tc.count()
if nextCount == oldCount:
break
print("TC has %i edges" % tc.count())
sc.stop()
这段代码在您的机器上表现不佳的原因有很多,但这很可能只是 中描述的问题的另一种变体。检查是否确实如此的最简单方法是在提交时提供 spark.default.parallelism
参数:
bin/spark-submit --conf spark.default.parallelism=2 \
examples/src/main/python/transitive_closure.py
如果不另行限定,SparkContext.union
、RDD.join
和RDD.union
将child的分区数设置为parents的分区总数。通常这是一种理想的行为,但如果反复应用,它可能会变得极其低效。
使用说明命令行是
transitive_closure [partitions]
设置默认并行度只会帮助每个分区中的连接,而不是初始工作分配。
我要争辩说应该使用更多的分区。设置默认并行度可能仍然有帮助,但您发布的代码明确设置了数字(传递的参数或 2,以较大者为准)。绝对最小值应该是 Spark 可用的内核,否则您的工作效率总是低于 100%。
我尝试使用 Spark 来解决简单的图形问题。我在 Spark 源文件夹中找到了一个示例程序:transitive_closure.py,它计算不超过 200 个边和顶点的图形中的传递闭包。但是在我自己的笔记本电脑上,它运行了 10 多分钟并且没有终止。我使用的命令行是:spark-submit transitive_closure.py。
我想知道为什么即使计算这么小的传递闭包结果,spark 也这么慢?这是一个常见的情况吗?有什么我想念的配置吗?
程序如下所示,可以在他们网站的spark install文件夹中找到。
from __future__ import print_function
import sys
from random import Random
from pyspark import SparkContext
numEdges = 200
numVertices = 100
rand = Random(42)
def generateGraph():
edges = set()
while len(edges) < numEdges:
src = rand.randrange(0, numEdges)
dst = rand.randrange(0, numEdges)
if src != dst:
edges.add((src, dst))
return edges
if __name__ == "__main__":
"""
Usage: transitive_closure [partitions]
"""
sc = SparkContext(appName="PythonTransitiveClosure")
partitions = int(sys.argv[1]) if len(sys.argv) > 1 else 2
tc = sc.parallelize(generateGraph(), partitions).cache()
# Linear transitive closure: each round grows paths by one edge,
# by joining the graph's edges with the already-discovered paths.
# e.g. join the path (y, z) from the TC with the edge (x, y) from
# the graph to obtain the path (x, z).
# Because join() joins on keys, the edges are stored in reversed order.
edges = tc.map(lambda x_y: (x_y[1], x_y[0]))
oldCount = 0
nextCount = tc.count()
while True:
oldCount = nextCount
# Perform the join, obtaining an RDD of (y, (z, x)) pairs,
# then project the result to obtain the new (x, z) paths.
new_edges = tc.join(edges).map(lambda __a_b: (__a_b[1][1], __a_b[1][0]))
tc = tc.union(new_edges).distinct().cache()
nextCount = tc.count()
if nextCount == oldCount:
break
print("TC has %i edges" % tc.count())
sc.stop()
这段代码在您的机器上表现不佳的原因有很多,但这很可能只是 spark.default.parallelism
参数:
bin/spark-submit --conf spark.default.parallelism=2 \
examples/src/main/python/transitive_closure.py
如果不另行限定,SparkContext.union
、RDD.join
和RDD.union
将child的分区数设置为parents的分区总数。通常这是一种理想的行为,但如果反复应用,它可能会变得极其低效。
使用说明命令行是
transitive_closure [partitions]
设置默认并行度只会帮助每个分区中的连接,而不是初始工作分配。
我要争辩说应该使用更多的分区。设置默认并行度可能仍然有帮助,但您发布的代码明确设置了数字(传递的参数或 2,以较大者为准)。绝对最小值应该是 Spark 可用的内核,否则您的工作效率总是低于 100%。