title: How to create a Row from a List or Array in Spark using Scala
body: <p>I'm trying to create a Row (<code>org.apache.spark.sql.catalyst.expressions.Row</code>) based on the user input. I'm not able to create a Row randomly. </p>
  
  <p><b>Is there any functionality to create a Row from <code>List</code> or <code>Array</code>.</b></p>
  
  <p>For eg., If I have a <code>.csv</code> file with the following format,</p>
  
  <pre><code>"91xxxxxxxxxx,21.31,15,0,0"
  </code></pre>
  
  <p>If the user input <code>[1, 2]</code> then I need to take only 2nd column and 3rd column along with the <code>customer_id</code> which is the first column</p>
  
  <p>I try to parse it with the code:</p>
  
  <pre><code>val l3 = sc.textFile("/SparkTest/abc.csv").map(_.split(" ")).map(r =&gt; (foo(input,r(0)))) `
  </code></pre>
  
  <p>where foo is defined as</p>
  
  <pre><code>def f(n: List[Int], s: String) : Row = {
      val n = input.length
      var out = new Array[Any](n+1)
      var r = s.split(",")
      out(0) = r(0)
      for (i &lt;- 1 to n)
          out(i) = r(input(i-1)).toDouble
      Row(out)
  }
  </code></pre>
  
  <p>and input is a List say </p>
  
  <pre><code>val input = List(1,2)
  </code></pre>
  
  <p>Executing this code I get l3 as:</p>
  
  <pre><code>Array[org.apache.spark.sql.Row] = Array([[Ljava.lang.Object;@234d2916])
  </code></pre>
  
  <p>But what I want is:</p>
  
  <pre><code>Array[org.apache.spark.sql.catalyst.expressions.Row] = Array([9xxxxxxxxxx,21.31,15])`
  </code></pre>
  
  <p>This has to be passed to create a schema in Spark SQL</p>