title: Apache Spark (Scala) - print 1 entry of an RDD / pairRDD
body: <p>When using an RDD I have grouped the items within the RDD by Key.</p>
  
  <pre><code>    val pairRDD = oldRDD.map(x =&gt; (x.user, x.product)).groupByKey
  </code></pre>
  
  <p><code>pairRDD</code> is of type: <code>RDD(Int, Iterable[Int]))</code></p>
  
  <p>What I am having trouble with is simply accessing a particular element. What is the point of having a key when I can't seemingly access the item in the RDD by key?</p>
  
  <p>At the minute I <code>filter</code> the RDD down to a single item, however I still have an RDD, and as such I have to do a <code>foreach</code> on the RDD to print it out:</p>
  
  <pre><code>    val userNumber10 = pairRDD.filter(_._1 == 10)
      userNumber10.foreach(x =&gt; println("user number = " + x._1))
  </code></pre>
  
  <p>Alternatively, I can <code>filter</code> the RDD and then <code>take(1)</code> which returns an Array of size 1:</p>
  
  <pre><code>    val userNumber10Array = pairRDD.filter(_._1 == 10).take(1)
  </code></pre>
  
  <p>Alternatively to that I can select the first element of that returned array:</p>
  
  <pre><code>    val userNumber10Array = pairRDD.filter(_._1 == 10).take(1)(0)
  </code></pre>
  
  <p>Which returns me the pair as required. But...
  clearly, this is inconvenient and I would hazard a guess at saying that this is not how an RDD is meant to be used!</p>
  
  <p>Why am I doing this you may ask! Well, the reason it's come about is because I simply wanted to "see" what was in my RDD for my own testing purposes. So, is there a way to access individual items in an RDD (more strictly a pairRDD) and if so, how? If not, what is the purpose of a pairRDD?</p>