Using ReduceByKey to group list of values
<p>I want to group list of values per key and was doing something like this:</p>

<pre><code>sc.parallelize(Array(("red", "zero"), ("yellow", "one"), ("red", "two"))).groupByKey().collect.foreach(println)

(red,CompactBuffer(zero, two))
(yellow,CompactBuffer(one))
</code></pre>

<p>But I noticed a blog post from Databricks and it's recommending not to use groupByKey for large dataset. </p>

<p><a href="https://databricks.gitbooks.io/databricks-spark-knowledge-base/content/best_practices/prefer_reducebykey_over_groupbykey.html" rel="nofollow">Avoid GroupByKey</a> </p>

<p>Is there a way to achieve the same result using reduceByKey?</p>

<p>I tried this but it's concatenating all values. By the way, for my case, both key and value are string type.</p>

<pre><code>sc.parallelize(Array(("red", "zero"), ("yellow", "one"), ("red", "two"))).reduceByKey(_ ++ _).collect.foreach(println)

(red,zerotwo)
(yellow,one)
</code></pre>