How does an RDD gets partitioned?
By default a partition is created for each HDFS partition, which by default is 64MB. Read more here.
How to balance my data across partitions?
First, take a look at the three ways one can repartition his data:
Pass a second parameter, the desired minimum number of partitions for your RDD, into textFile(), but be careful:
In : lines = sc.textFile("data")
In : lines.getNumPartitions() Out: 1000
In : lines = sc.textFile("data", 500)
In : lines.getNumPartitions() Out: 1434
In : lines = sc.textFile("data", 5000)
In : lines.getNumPartitions() Out: 5926
As you can see,
 doesn't do what one would expect, since the number of partitions the RDD has, is already greater than the minimum number of partitions we request.
Use repartition(), like this:
In : lines = lines.repartition(10)
In : lines.getNumPartitions() Out: 10
Warning: This will invoke a shuffle and should be used when you want to increase the number of partitions your RDD has.
From the docs:
The shuffle is Spark’s mechanism for re-distributing data so that it’s grouped differently across partitions. This typically involves copying data across executors and machines, making the shuffle a complex and costly operation.
Use coalesce(), like this:
In : lines = lines.coalesce(2)
In : lines.getNumPartitions() Out: 2
Here, Spark knows that you will shrink the RDD and gets advantage of it. Read more about repartition() vs coalesce().
But will all this guarantee that your data will be perfectly balanced across your partitions? Not really, as I experienced in How to balance my data across the partitions?