title: Link and Flatten PairRDD based on common element in key or value
body: <p>I have a RDD of Pairs as below :</p>
  
  <pre><code>(a,b)
  (b,c)
  (e,d)
  (f,d)
  (g,f)
  </code></pre>
  
  <p>I would like to find items that can be linked together based on either the key or value, flatten them and create a sorted RDD. For example after the 
  transformation the new RDD will be :</p>
  
  <pre><code>(a,b,c) - As (a,b) and (b,c) can be linked by common element "b"
  (d,e,f,g) - As (e,d) and (f,d) is linked by "d", (f,d) and (g,f) is linked by "f"
  </code></pre>
  
  <p>For an input like :</p>
  
  <pre><code>(a,b),(b,c),(c,d),(a,d),(c,e)
  </code></pre>
  
  <p>The output should be:</p>
  
  <pre><code>(a,b,c,d,e) - As all the pairs are connected by either the key or value with some other pair
  </code></pre>
  
  <p>Appreciate any ideas on implementing a scalable solution for this using Apache Spark.</p>