2
后,我映射我RDD到如何使用reduceByKey将值添加到Scala Spark中的Set中?
((_id_1, section_id_1), (_id_1, section_id_2), (_id_2, section_3), (_id_2, section_4))
我想reduceByKey
到
((_id_1, Set(section_id_1, section_id_2), (_id_2, Set(section_3, section_4)))
val collectionReduce = collection_filtered.map(item => {
val extras = item._2.get("extras")
var section_id = ""
var extras_id = ""
if (extras != null) {
val extras_parse = extras.asInstanceOf[BSONObject]
section_id = extras_parse.get("guid").toString
extras_id = extras_parse.get("id").toString
}
(extras_id, Set {section_id})
}).groupByKey().collect()
我的输出
((_id_1, (Set(section_1), Set(section_2))), (_id_2, (Set(section_3), Set(section_4))))
我该如何解决呢?
在此先感谢。
上面使用reduceByKey和你的方式有什么不同?哪一个更好?谢谢。 – giaosudau
'groupByKey'在这里更简单,因为您不会聚合(“* reduce *”)具有相同键的两个键值对之间的任何信息:只需将这些值连接在一起。使用reduce的实现将做同样的工作,只是稍微难以阅读版本。 – huitseeker
您应该比'groupByKey'更优先使用'reduceByKey',看看这个[Spark gitbook](http://databricks.gitbooks.io/databricks-spark-knowledge-base/content/best_practices/prefer_reducebykey_over_groupbykey.html)说明。 –