title: How to transform RDD[(Key, Value)] into Map[Key, RDD[Value]]
body: <p>I searched a solution for a long time but didn't get any correct algorithm.</p>
  
  <p>Using Spark RDDs in scala, how could I transform a <code>RDD[(Key, Value)]</code> into a <code>Map[key, RDD[Value]]</code>, knowing that I can't use collect or other methods which may load data into memory ?</p>
  
  <p>In fact, my final goal is to loop on <code>Map[Key, RDD[Value]]</code> by key and call <code>saveAsNewAPIHadoopFile</code> for each <code>RDD[Value]</code></p>
  
  <p>For example, if I get :</p>
  
  <pre><code>RDD[("A", 1), ("A", 2), ("A", 3), ("B", 4), ("B", 5), ("C", 6)]
  </code></pre>
  
  <p>I'd like :</p>
  
  <pre><code>Map[("A" -&gt; RDD[1, 2, 3]), ("B" -&gt; RDD[4, 5]), ("C" -&gt; RDD[6])]
  </code></pre>
  
  <p>I wonder if it would cost not too much to do it using <code>filter</code> on each key A, B, C of <code>RDD[(Key, Value)]</code>, but I don't know if calling filter as much times there are different keys would be efficient ? (off course not, but maybe using <code>cache</code> ?)</p>
  
  <p>Thank you</p>