【发布时间】:2015-03-26 14:15:58
【问题描述】:
我将 spark 与 scala 一起使用,并且我有一个充满 tuple2 的 RDD,其中包含一个复杂对象作为键和一个双精度对象。目的是在对象相同的情况下添加双倍(频率)。
为此,我将我的对象定义如下:
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 特征是告诉 Spark 如何比较我的对象的好方法吗? 我应该只使用 tuple2[String,Double] 吗?
谢谢,
【问题讨论】:
标签: scala apache-spark reduce