简体   繁体   English

使用Spark在RDD的每个分区中使用固定的最小元素数进行分区

[英]Repartition with a fixed minimum number of elements in each partition of the RDD using Spark

I have a RDD with the following number of elements in each partition (total number of partitions is val numPart = 32 : 我在每个分区中都有一个具有以下元素数量的RDD(分区总数为val numPart = 32

1351, 962, 537, 250, 80, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 88, 270, 635, 1028, 1388, 1509 1351、962、537、250、80、9、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0, 15,88,270,635,1028,1388,1509

To see the previous output I use this: 要查看先前的输出,我使用以下命令:

def countByPartition[A](anRdd: RDD[A]): RDD[Int] = anRdd.mapPartitions(iter => Iterator(iter.length))

println(countByPartition(anRdd).collect.mkString(", "))

I would like to have on each partition at least a minimum number of elements given by val min = 5 . 我想在每个分区上至少有val min = 5给出的最小数量的元素。

I've tried to perform anRdd.repartition(numPart) and I get the following: 我尝试执行anRdd.repartition(numPart)并得到以下信息:

257, 256, 256, 256, 255, 255, 254, 253, 252, 252, 252, 252, 252, 252, 252, 252, 251, 250, 249, 248, 248, 248, 248, 248, 261, 261, 260, 260, 259, 258, 258, 257 257、256、256、256、255、255、254、253、252、252、252、252、252、252、252、252、251、250、249、248、248、248、248、248、261, 261、260、260、259、258、258、257

In this case, it was perfect because in each partition I have more than min elements. 在这种情况下,这是完美的,因为在每个分区中,我都有多个min元素。 But it doesn't always gets the same and sometimes I get some partitions with values less than min value. 但这并不总是一样,有时我会得到一些分区,这些分区的值小于min

Is there a way to do what I want? 有什么方法可以做我想要的吗?

It is not possible and in general you need to choose partitioning so that the sizes are roughly even. 这是不可能的,通常您需要选择分区,以使大小大致均匀。 Partitioners in Spark basically implement two methods numPartitions and getPartition . Spark中的分区程序基本上实现了两种方法numPartitionsgetPartition The latter is a function from a single key to a partition number so other elements and thus the potential size of partitions are not known at this point. 后者是从单个键到分区号的功能,因此此时尚不了解其他元素以及分区的潜在大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM