Spark grouping tuning
<p>Given following files:</p>

<ul>
<li>employees</li>
<li>skills</li>
<li>reports</li>
<li>etc. </li>
</ul>

<p>Between employees and each of the other files there is 1-to-N relationship, e.g. to one employee correspond multiple skills. Each file is between 500MB to 1.5GB and there are about 10 files in total.
For each employee, I want to aggregate/gather all the information from all the files (skills, reports, etc.) and write it to an XML structure:</p>

<pre><code>&lt;employees&gt;
  &lt;employee&gt;
    &lt;skills&gt;
      &lt;skill&gt;...&lt;/skill&gt;
      &lt;skill&gt;...&lt;/skill&gt;
      ...
    &lt;/skills&gt;
    &lt;reports
      &lt;report&gt;...&lt;/report&gt;
      &lt;report&gt;...&lt;/report&gt;
      ...
    &lt;/reports&gt;
    ...
  &lt;/employee&gt;
  ...
&lt;/employees&gt;
</code></pre>

<p>I'm doing something along the lines:</p>

<pre><code>val employeesRdd = employeesDf.map(r =&gt; (r.getAs[String]("employeeId"), r))
val skillsRdd = skillsDf.map(r =&gt; (r.getAs[String]("employeeId"), r)).groupByKey()
val reportsRdd = reportsDf.map(r =&gt; (r.getAs[String]("employeeId"), r)).groupByKey()
...

employeesRdd
  .leftOuterJoin(skillsRdd)
  .leftOuterJoin(reportsRdd)
  ...
  .toLocalIterator
  ... // write &lt;employee&gt; nodes one by one
</code></pre>

<p>Problem I have is that all groupByKey() operations are very slow, as in many hours long. And after running for so long it blows up due to java.lang.OutOfMemoryError: GC overhead limit exceeded. I'm using Spark 1.5.1 in local mode with about 20GB assigned to the jvm.</p>