title: How to get keys and values from MapType column in SparkSQL DataFrame
body: <p>I have data in a parquet file which has 2 fields: <code>object_id: String</code> and <code>alpha: Map&lt;&gt;</code>.</p>
  
  <p>It is read into a data frame in sparkSQL and the schema looks like this:</p>
  
  <pre><code>scala&gt; alphaDF.printSchema()
  root
   |-- object_id: string (nullable = true)
   |-- ALPHA: map (nullable = true)
   |    |-- key: string
   |    |-- value: struct (valueContainsNull = true)
  </code></pre>
  
  <p>I am using Spark 2.0 and I am trying to create a new data frame in which columns need to be <code>object_id</code> plus keys of the <code>ALPHA</code> map as in <code>object_id, key1, key2, key2, ...</code> </p>
  
  <p>I was first trying to see if I could at least access the map like this:</p>
  
  <pre><code>scala&gt; alphaDF.map(a =&gt; a(0)).collect()
  &lt;console&gt;:32: error: Unable to find encoder for type stored in a Dataset.
  Primitive types (Int, String, etc) and Product types (case classes) are 
  supported by importing spark.implicits._  Support for serializing other
  types will be added in future releases.
     alphaDF.map(a =&gt; a(0)).collect()
  </code></pre>
  
  <p>but unfortunately I can't seem to be able to figure out how to access the keys of the map.</p>
  
  <p>Can someone please show me a way to get the <code>object_id</code> plus map keys as column names and map values as respective values in a new dataframe?</p>