I load a dataset
val data = sc.textFile("/home/kybe/Documents/datasets/img.csv",defp)
I want to put an index on this data thus
val nb = data.count.toInt
val tozip = sc.parallelize(1 to nb).repartition(data.getNumPartitions)
val res = tozip.zip(data)
Unfortunately i have the following error
Can only zip RDDs with same number of elements in each partition
How can i modify the number of element by partition if it is possible ?
The documentation for zip() states:
Zips this RDD with another one, returning key-value pairs with the first element in each RDD, second element in each RDD, etc. Assumes that the two RDDs have the same number of partitions and the same number of elements in each partition (e.g. one was made through a map on the other).
So we need to make sure we meet 2 conditions:
You are making sure that you will have the same number of partitions with repartition()
but Spark doesn't guarantee that you will have the same distribution in each partition for each RDD.
Because there are different types of RDDs and most of them have different partitioning strategies! For example:
sc.parallelize(collection)
it will see how many partitions there should be, will check the size of the collection and calculate the step
size. I.e. you have 15 elements in the list and want 4 partitions, first 3 will have 4 consecutive elements last one will have the remaining 3.<Long, Text>
and you just want String
:-)In your example Spark internally does create different types of RDDs (CoalescedRDD
and ShuffledRDD
) while doing the repartitioning but I think you got the global idea that different RDDs have different partitioning strategies :-)
Notice that the last part of the zip()
doc mentions the map()
operation. This operation does not repartition as it's a narrow transformation data so it would guarantee both conditions.
In this simple example as it was mentioned you can do simply data.zipWithIndex
. If you need something more complicated then creating the new RDD for zip()
should be created with map()
as mentioned above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With