Spark: TOPN after GroupBy
<p>I have an RDD <strong>P</strong> mapped to the class:</p>

<pre><code>case class MyRating(userId:Int, itemId:Int, rating:Double)
</code></pre>

<p>I am interested in finding TopN entries for each User i.e. GroupBy <strong>userId</strong> and within each formed group, filter out the TopN (say 10) entries based on highest <strong>rating</strong>.</p>

<p>I did the following:</p>

<pre><code>val A : RDD[((Int), Iterable[MyRating])] = P.keyBy(r =&gt; (r.userId)).groupByKey
val B : RDD[((Int), List[MyRating])] = key.mapValues(iter =&gt; iter.toList.sortBy(_.rating, false))
val C = values.groupByKey.take(10)
</code></pre>

<p>Clearly applying .take(10) after groupByKey leaves me with only 10 keys(Users) and will not filter out the top10 ratings for each User.</p>

<p>How do we go about applying .take(N) after a groupBy so that it acts on some part of value instead of key itself?</p>