title: Top N items from a Spark DataFrame/RDD
body: <p>My requirement is to get the top N items from a dataframe.</p>
  
  <p>I've this DataFrame: </p>
  
  <pre><code>val df = List(
        ("MA", "USA"),
        ("MA", "USA"),
        ("OH", "USA"),
        ("OH", "USA"),
        ("OH", "USA"),
        ("OH", "USA"),
        ("NY", "USA"),
        ("NY", "USA"),
        ("NY", "USA"),
        ("NY", "USA"),
        ("NY", "USA"),
        ("NY", "USA"),
        ("CT", "USA"),
        ("CT", "USA"),
        ("CT", "USA"),
        ("CT", "USA"),
        ("CT", "USA")).toDF("value", "country")
  </code></pre>
  
  <p>I was able to map it to an <code>RDD[((Int, String), Long)]</code>
  colValCount: 
  Read: ((colIdx, value), count)</p>
  
  <pre><code>((0,CT),5)
  ((0,MA),2)
  ((0,OH),4)
  ((0,NY),6)
  ((1,USA),17)
  </code></pre>
  
  <p>Now I need to get the top 2 items for each column index. So my expected output is this:</p>
  
  <pre><code>RDD[((Int, String), Long)]
  
  ((0,CT),5)
  ((0,NY),6)
  ((1,USA),17)
  </code></pre>
  
  <p>I've tried using freqItems api in DataFrame but it's slow.</p>
  
  <p>Any suggestions are welcome.</p>