title: Convert RDD of Array(Row) to RDD of Row?
body: <p>I have such data in a file and I'd like to do some statistics using Spark.</p>
  
  <p>File content:</p>
  
  <pre><code>aaa|bbb|ccc
  ddd|eee|fff|ggg
  </code></pre>
  
  <p>I need to assign each line an id. I read them as rdd and use <code>zipWithIndex()</code>.</p>
  
  <p>Then they should be like: </p>
  
  <pre><code>(0, aaa|bbb|ccc)
  (1, ddd|eee|fff|ggg)
  </code></pre>
  
  <p>I need to make each string associated with the id. I can get the RDD of Array(Row), but can't jump out of the array.</p>
  
  <p>How should I modify my code?</p>
  
  <pre class="lang-scala prettyprint-override"><code>import org.apache.spark.sql.{Row, SparkSession}
  
  val fileRDD = spark.sparkContext.textFile(filePath)
  val fileWithIdRDD = fileRDD.zipWithIndex()
  // make the line like this: (0, aaa), (0, bbb), (0, ccc)
  // each line is a record of Array(Row)
  fileWithIdRDD.map(x =&gt; {
    val id = x._1
    val str = x._2
    val strArr = str.split("\\|")
    val rowArr = strArr.map(y =&gt; {
      Row(id, y)
    }) 
    rowArr 
  })
  </code></pre>
  
  <p>Now it looks like: </p>
  
  <pre><code>[(0, aaa), (0, bbb), (0, ccc)]
  [(1, ddd), (1, eee), (1, fff), (1, ggg)]
  </code></pre>
  
  <p>But finally I want:</p>
  
  <pre><code>(0, aaa)
  (0, bbb) 
  (0, ccc)
  (1, ddd)
  (1, eee)
  (1, fff)
  (1, ggg)
  </code></pre>