title: Convert Dataframe to a Map(Key-Value) in Spark
body: <p>So, I have a DataFrame in Spark which looks like this:</p>
  
  <p>It has 30 columns: only showing some of them!</p>
  
  <pre><code>[ABCD,color,NORMAL,N,2015-02-20,1]
  [XYZA,color,NORMAL,N,2015-05-04,1]
  [GFFD,color,NORMAL,N,2015-07-03,1]
  [NAAS,color,NORMAL,N,2015-08-26,1]
  [LOWW,color,NORMAL,N,2015-09-26,1]
  [KARA,color,NORMAL,N,2015-11-08,1]
  [ALEQ,color,NORMAL,N,2015-12-04,1]
  [VDDE,size,NORMAL,N,2015-12-23,1]
  [QWER,color,NORMAL,N,2016-01-18,1]
  [KDSS,color,NORMAL,Y,2015-08-29,1]
  [KSDS,color,NORMAL,Y,2015-08-29,1]
  [ADSS,color,NORMAL,Y,2015-08-29,1]
  [BDSS,runn,NORMAL,Y,2015-08-29,1]
  [EDSS,color,NORMAL,Y,2015-08-29,1]
  </code></pre>
  
  <p>So, I have to convert this dataFrame into a key-Value Pair in Scala, using the key as some of the columns in the Dataframe and assigning unique values to those keys from index 0 to the count(distinct number of keys).</p>
  
  <p>For example: using the case above, I want to have an output in a map(key-value) collection in Scala like this:</p>
  
  <pre><code>    ([ABC_color_NORMAL_N_1-&gt;0]
      [XYZA_color_NORMAL_N_1-&gt;1]
      [GFFD_color_NORMAL_N_1-&gt;2]
      [NAAS_color_NORMAL_N_1-&gt;3]
      [LOWW_color_NORMAL_N_1-&gt;4]
      [KARA_color_NORMAL_N_1-&gt;5]
      [ALEQ_color_NORMAL_N_1-&gt;6]
      [VDDE_size_NORMAL_N_1-&gt;7]
      [QWER_color_NORMAL_N_1-&gt;8]
      [KDSS_color_NORMAL_Y_1-&gt;9]
      [KSDS_color_NORMAL_Y_1-&gt;10]
      [ADSS_color_NORMAL_Y_1-&gt;11]
      [BDSS_runn_NORMAL_Y_1-&gt;12]
      [EDSS_color_NORMAL_Y_1-&gt;13]
      )
  </code></pre>
  
  <p>I'm new to Scala and Spark and I tried doing something Like this.</p>
  
  <pre><code> var map: Map[String, Int] = Map()
      var i = 0
      dataframe.foreach( record =&gt;{
      //Is there a better way of creating a key!
          val key = record(0) + record(1) + record(2) + record(3)
          var index = i
          map += (key -&gt; index)
          i+=1
            }
          )
  </code></pre>
  
  <p>But, this is not working.:/ The Map is null after this completes.</p>