title: Apache Spark: get elements of Row by name
body: <p>In a <code>DataFrame</code> object in Apache Spark (I'm using the Scala interface), if I'm iterating over its <code>Row</code> objects, is there any way to extract values by name?  I can see how to do some really awkward stuff:</p>
  
  <pre><code>def foo(r: Row) = {
    val ix = (0 until r.schema.length).map( i =&gt; r.schema(i).name -&gt; i).toMap
    val field1 = r.getString(ix("field1"))
    val field2 = r.getLong(ix("field2"))
    ...
  }
  dataframe.map(foo)
  </code></pre>
  
  <p>I figure there must be a better way - this is pretty verbose, it requires creating this extra structure, and it also requires knowing the types explicitly, which if incorrect, will produce a runtime exception rather than a compile-time error.</p>