title: How to write an UDF in Spark to map indexes to string labels?
body: <p>I am using Spark and I have a table that has a specific string format in one of the columns called <code>predictions</code>. The format is always of the type - <code>0=some_probability,1=some_other_probability,2=some_other_probability</code> . </p>
  
  <p>Here are a few sample records from that table - </p>
  
  <pre><code>val table1 = Seq(
  ("0=0.5,1=0.3,2=0.2"),
  ("0=0.6,1=0.2,2=0.2"),
  ("0=0.1,1=0.1,2=0.8")
  ).toDF("predictions")
  
  table1.show(false)
  +-----------------+
  |predictions      |
  +-----------------+
  |0=0.5,1=0.3,2=0.2|
  |0=0.6,1=0.2,2=0.2|
  |0=0.1,1=0.1,2=0.8|
  +-----------------+
  </code></pre>
  
  <p>Now, I also have metadata information about each of these indexes - <code>0,1,2...n</code> in a separate string. The metadata string looks like - </p>
  
  <pre><code>val metadata = "AA::BB::CC"
  </code></pre>
  
  <p>I would like to write a UDF in Scala to map these indexes to each element in the string. The output of that UDF should give me a new column which looks like this - </p>
  
  <pre><code>+--------------------+
  |labelled_predictions|      
  +--------------------+
  |AA=0.5,BB=0.3,CC=0.2|
  |AA=0.6,BB=0.2,CC=0.2|
  |AA=0.1,BB=0.1,CC=0.8|
  +--------------------+
  </code></pre>
  
  <p>So, <code>0</code> is replaced by <code>AA</code> since <code>AA</code> is the first element in the <code>metadata</code> string that is always split by <code>::</code>.</p>
  
  <p>How do I write an UDF in Scala-Spark to do this ?</p>