title: How to create collection of RDDs out of RDD?
body: <p>I have an <code>RDD[String]</code>, <code>wordRDD</code>. I also have a function that creates an RDD[String] from a string/word. I would like to create a new RDD <strong>for each string</strong> in <code>wordRDD</code>. Here are my attempts:</p>
  
  <p>1) Failed because Spark does not support nested RDDs:</p>
  
  <pre><code>var newRDD = wordRDD.map( word =&gt; {
    // execute myFunction()
    (new MyClass(word)).myFunction()
  })
  </code></pre>
  
  <p>2) Failed (possibly due to scope issue?):</p>
  
  <pre><code>var newRDD = sc.parallelize(new Array[String](0))
  val wordArray = wordRDD.collect
  for (w &lt;- wordArray){
    newRDD = sc.union(newRDD,(new MyClass(w)).myFunction())
  }
  </code></pre>
  
  <p>My ideal result would look like:</p>
  
  <pre><code>// input RDD (wordRDD)
  wordRDD: org.apache.spark.rdd.RDD[String] = ('apple','banana','orange'...)
  
  // myFunction behavior
  new MyClass('apple').myFunction(): RDD[String] = ('pple','aple'...'appl')
  
  // after executing myFunction() on each word in wordRDD:
  newRDD: RDD[String] = ('pple','aple',...,'anana','bnana','baana',...)
  </code></pre>
  
  <p>I found a relevant question here: <a href="https://stackoverflow.com/questions/30522564/spark-when-union-a-lot-of-rdd-throws-stack-overflow-error">Spark when union a lot of RDD throws stack overflow error</a>, but it didn't address my issue.</p>