title: How to Group By many keys in Spark RDD?
body: <p>Imagine I have a triplet : </p>
  
  <pre><code>val RecordRDD : RDD[Int, String, Int] = {
  
                  (5 , "x1", 100),
                  (3 , "x2", 200),
                  (3 , "x4", 300),
                  (5 , "x1", 150),
                  (3 , "x2", 160),
                  (5 , "x1", 400)
    }
  </code></pre>
  
  <p>How can I efficiently group them by the first two elements and sort by the third? For example make it:</p>
  
  <pre><code>                [5 , [  "x1" -&gt; [100, 150, 400]  ]
                  [3 , [   ["x2" -&gt; [160, 200]],   ["x4" -&gt; [300]]    ]
  </code></pre>
  
  <p>I am looking for an efficient way.</p>
  
  <p>Should I make it a DataFrame and make use of GroupBy(Col1,Col2) and SortBy(Col3)?</p>
  
  <p>Would that be more efficient than groupBy of Spark RDD?</p>
  
  <p>Can AggregateByKey aggregate on 2 keys simultaneously?</p>
  
  <p>*You can suppose this RDD is pretty large! Thanks in advance. </p>