我正在使用Scala的Spark,并且我有一个RDD,其中包含一个复杂对象作为键和一个double值的tuple2。目标是如果对象相同,则将double(频率)相加。
为此,我已经定义了以下对象:
case class SimpleCoocurrence(word:String, word_pos:String, cooc:String, cooc_pos:String, distance:Double) extends Ordered[SimpleCoocurrence]{
def compare(that: SimpleCoocurrence) = {
if(this.word.equals(that.word)&&this.word_pos.equals(that.word_pos)
&&this.cooc.equals(that.cooc)&&this.cooc_pos.equals(that.cooc_pos))
0
else
this.toString.compareTo(that.toString)
}
}
现在我正在尝试使用reduceByKey,代码如下:
val coocRDD = sc.parallelize(coocList)
println(coocRDD.count)
coocRDD.map(tup=>tup).reduceByKey(_+_)
println(coocRDD.count)
但是结果表明,在reduceByKey处理前后,RDD包含的元素数量完全相同。
如何使用tuple2 [SimpleCoocurrence,Double]执行reduceByKey? 实现有序特征(Ordered trait)是告诉Spark如何比较对象的好方法吗? 我应该仅使用tuple2 [String, Double]吗?
谢谢!