Python Spark在还原时不进行任何平行化
我是个新手。我正在使用python(pyspark)编写我的程序。我使用Python Spark在还原时不进行任何平行化,python,scala,bigdata,apache-spark,Python,Scala,Bigdata,Apache Spark,我是个新手。我正在使用python(pyspark)编写我的程序。我使用groupByKey函数将键值对转换为键-(值列表)对。我在一台64核的计算机上运行spark,我尝试通过使用以下命令启动程序来实现所有64核的utlize spark-submit --master local[64] my_program.py 但是,我注意到,在执行groupByKey函数时,只使用了一个内核。数据相当大。那么,为什么spark不将其划分为64个分区,并在64个不同的内核中进行缩减/分组呢 我是否错过
groupByKey
函数将键值对转换为键-(值列表)对。我在一台64核的计算机上运行spark,我尝试通过使用以下命令启动程序来实现所有64核的utlize
spark-submit --master local[64] my_program.py
但是,我注意到,在执行groupByKey
函数时,只使用了一个内核。数据相当大。那么,为什么spark不将其划分为64个分区,并在64个不同的内核中进行缩减/分组呢
我是否错过了并行化的一些重要步骤
代码的相关部分如下所示
# Here input itself is a key-(list of values) pair. The mapPartitions
# function is used to return a key-value pair (variable x), from
# which another key-(list of values) pair is created (variable y)
x = input.mapPartitions(transFunc)
# x contains key value pair, such as [(k1, v1), (k1, v2), (k2, v3)]
y = x.groupByKey()
# y contains key-list of values pair such as [(k1, [v1, v2]), (k2, [v2])]
Spark中的默认并行度级别由配置选项决定:
Spark.default.parallelism
。默认值为:(*来自)
本地模式:本地机器Mesos细粒度上的芯数
模式:8其他:所有executor节点上的核心总数或2,
以较大者为准
可以使用以下操作在更多或更少的分区中重新组合RDD:
rdd.repartition(partitions: Int) // redistributes the RDD into the given nr of partitions
rdd.coalesce(partitions:Int) // reduces the number of partitions of the RDD to the given nr
需要内部洗牌的操作通常使用numPartitions
参数来指定目标分区的数量。在这样的操作之后,RDD将拥有新数量的分区。
让我用一个例子来说明这一点:
鉴于:
val rdd = sc.textFile("localFile") // default nr of partitions. Let's say 2
然后:
您是如何加载数据的?@maasg:我使用mapPartitions。在mapPartitions之后,变量x中的结果数据是一个键值对,其中key是一个字符串,value也是一个字符串。然后,我使用groupByKey形成一个键到(值列表)对,其中键与x中的键相同,值列表是字符串值列表。您可以将代码添加到问题中吗?@maasg:现在添加了代码。仍然缺少加载数据的部分。
val moreParallelRdd = rdd.repartition(64) // 64 partitions
val onePartitionRdd = moreParallelRdd.coalesce(1) // 1 partition
val sortedRdd = onePartitionRdd.sortBy(x=> sortSelector(x), numPartitions=10) // 10 partitions