title: Can we force spark to save partitions, from two PairRDDs, with the same key on the same machines?
body: <p>I am working with Spark on top of a HDFS cluster.
  Before a join operation in Java Spark between two (key,value) PairRDDs, I partition data of both files with a HashPartitioner to have elements with the same key on the same machine. That's fine for each file independently, but as mentioned in a previous post <a href="https://stackoverflow.com/questions/34368202/when-create-two-different-spark-pair-rdd-with-same-key-set-will-spark-distribut">When create two different Spark Pair RDD with same key set, will Spark distribute partition with same key to the same machine?</a>, partitioned RDDs are not necessarily co-located, so the same keys from both RDDs may not be on the same machine.</p>
  
  <p>Is there a way to force spark to do that? to be sure that same keys from different PairRDDs are on the same machine</p>
  
  <p>Btw since we use a hashpartitioning, for both RDDs same keys are in the same partition number but the order of the machines on which partitions are saved is different from one RDD to the other.</p>
  
  <p>For example, if we have 'N' machines, is there a way to attribute a number to each machine (I guess spark do this internally),
  and then simply force the partition number 'P' to be written on the machine 'N modulo P'?</p>