title: CSV to Array of Objects
body: <p>I am using a third party package for spark that utilizes a "PointFeature" object. I am trying to take a csv file and put elements form each row into an Array of these PointFeature objects. </p>
  
  <p>The PointFeature constructor for my implementation looks like this: </p>
  
  <pre><code>Feature(Point( _c1, _c2), _c3)
  </code></pre>
  
  <p>where _c1, _c2, and _c3 are the columns of my csv and represent doubles. </p>
  
  <p>Here is my current attempt:</p>
  
  <pre><code>val points: Array[PointFeature[Double]] = for{
      line &lt;- sc.textFile("file.csv")
      point &lt;- Feature(Point(line._c1,line._c2),line._c3)
  } yield point
  </code></pre>
  
  <p>My error shows up when referencing the columns</p>
  
  <pre><code>&lt;console&gt;:36: error: value _c1 is not a member of String
     point &lt;- Feature(Point(line._c1,line._c2),line._c3.toDouble)
                                 ^
  &lt;console&gt;:36: error: value _c2 is not a member of String
         point &lt;- Feature(Point(line._c1,line._c2),line._c3.toDouble)
                                              ^
  &lt;console&gt;:36: error: value _c3 is not a member of String
         point &lt;- Feature(Point(line._c1,line._c2),line._c3.toDouble)
                                                        ^
  </code></pre>
  
  <p>This is obviously because I'm referencing a String as if it were an element of a DataFrame. I'm wondering if there is a way to work with DataFrames in this loop format, or a way to split each line into a List of doubles. Maybe I need an RDD?  </p>
  
  <p>Also, I'm not certain that this will yield an Array. Actually, I suspect it will return an RDD...</p>
  
  <p>I am using Spark 2.1.0 on Amazon EMR </p>
  
  <p>Here are some other Questions I have drawn from:</p>
  
  <p><a href="https://stackoverflow.com/questions/32055496/how-to-read-csv-file-into-an-array-of-arrays-in-scala">How to read csv file into an Array of arrays in scala</a></p>
  
  <p><a href="https://stackoverflow.com/questions/29820501/splitting-strings-in-apache-spark-using-scala">Splitting strings in Apache Spark using Scala</a></p>
  
  <p><a href="https://stackoverflow.com/questions/33030726/how-to-iterate-records-spark-scala">How to iterate records spark scala?</a></p>