title: Fetch lines based on regex match along with the filenames from a directory using apache spark
body: <p>Given a regex and a hdfs directory, I need to list all the filenames with matching lines where the regex occurs. I am trying to do this using apache <strong>spark</strong>. Language <strong>scala</strong> preferable.</p>
  
  <p>I need the return value somewhat in this format : </p>
  
  <p>List&lt; Map&lt; filename:String,List&lt; line:String> > ></p>
  
  <p>by the following approach I can list all the file names.</p>
  
  <pre><code>val files = sc.wholeTextFiles(dirPath)
  val regexpr = regex.r
  var filter = files.filter{case(filename, content) =&gt; regexpr.findAllIn(content).length &gt; 0 }
  </code></pre>
  
  <p>but I am not being able to fetch the exact lines where the regex occurs.</p>