title: Splitting strings in Apache Spark using Scala
body: <p>I have a dataset, which contains lines in the format (tab separated):</p>
  
  <pre><code>Title&lt;\t&gt;Text
  </code></pre>
  
  <p>Now for every word in <code>Text</code>, I want to create a <code>(Word,Title)</code> pair. 
  For instance:</p>
  
  <pre><code>ABC      Hello World
  </code></pre>
  
  <p>gives me</p>
  
  <pre><code>(Hello, ABC)
  (World, ABC)
  </code></pre>
  
  <p>Using Scala, I wrote the following:</p>
  
  <pre><code>val file = sc.textFile("s3n://file.txt")
  val title = file.map(line =&gt; line.split("\t")(0))
  val wordtitle = file.map(line =&gt; (line.split("\t")(1).split(" ").map(word =&gt; (word, line.split("\t")(0)))))
  </code></pre>
  
  <p>But this gives me the following output:</p>
  
  <pre><code>[Lscala.Tuple2;@2204b589
  [Lscala.Tuple2;@632a46d1
  [Lscala.Tuple2;@6c8f7633
  [Lscala.Tuple2;@3e9945f3
  [Lscala.Tuple2;@40bf74a0
  [Lscala.Tuple2;@5981d595
  [Lscala.Tuple2;@5aed571b
  [Lscala.Tuple2;@13f1dc40
  [Lscala.Tuple2;@6bb2f7fa
  [Lscala.Tuple2;@32b67553
  [Lscala.Tuple2;@68d0b627
  [Lscala.Tuple2;@8493285
  </code></pre>
  
  <p>How do I solve this? </p>
  
  <p><strong>Further reading</strong></p>
  
  <p>What I want to achieve is to count the number of <code>Words</code> that occur in a <code>Text</code> for a particular <code>Title</code>.</p>
  
  <p>The subsequent code that I have written is:</p>
  
  <pre><code>val wordcountperfile = file.map(line =&gt; (line.split("\t")(1).split(" ").flatMap(word =&gt; word), line.split("\t")(0))).map(word =&gt; (word, 1)).reduceByKey(_ + _)
  </code></pre>
  
  <p>But it does not work. Please feel free to give your inputs on this. Thanks!</p>