title: Scala Spark - how to iterate fields in a Dataframe
body: <p>My Dataframe has several columns with different types (string, double, Map, array, etc).</p>
  
  <p>I need to perform some operation in certain column types and I am looking for a nice way to identify the field type and then do the proper action</p>
  
  <p>types: <code>String|Double|Map&lt;String,Int&gt;|...</code></p>
  
  <pre><code>|---------------------------------------------------------------
  |myString1 |myDouble1|     myMap1                   | ...otherTypes                          
  |---------------------------------------------------------------
  |"string_1"|  123.0  |{"str1Map":1,"str2":2, "str31inmap": 31} |...
  |"string_2"|  456.0  |{"str2Map":2,"str22":2, "str32inmap": 32}|...
  |"string_3"|  789.0  |{"str3Map":3,"str23":2, "str33inmap": 33}|...
  |---------------------------------------------------------------
  </code></pre>
  
  <p>Iterating the dataframe fields and printing: <code>df.schema.fields.foreach { println }</code></p>
  
  <p>outputs:</p>
  
  <pre><code>StructField(myString1,StringType,true)
  StructField(myDouble1,DoubleType,false)
  StructField(myMap1,MapType(StringType,IntType,false),true)
  ...
  StructField(myStringList,ArrayType(StringType,true),true)
  </code></pre>
  
  <p>So, my  idea is to iterate through the fields and in case is one of the types that I need to perform an operation (e.g. on the Map type), then I know the field name/column and action to take.</p>
  
  <pre><code> df.schema.fields.foreach { f =&gt;
       val fName = ?get the name
       val fType = ?get the Type
       print("Name{} Type:{}".format(fName , fType))
  
        // case type is Map do action X
        // case type is Stringdo action Y
        // ...
  
      }
  </code></pre>
  
  <p>Does this approach makes sense to detect the field types on my dataframe and then perform different on the df fields, depending on their type?
  How to get it to work?</p>