使用spark 2,我希望使用一个case类对象作为元组的键来进行连接或通过键操作进行缩减,但它似乎无法按照我迄今为止能够探索的方式工作。请参阅下面的示例。鲍勃应该是2作为最终输出,但每个人的处理方式与你在最后看到的不同: -
case class P(name:String)
{
override def hashCode:Int = { name.hashCode() }
override def equals(obj:Any):Boolean = {
obj match {
case x:P => x.name.contentEquals(this.name)
case _ => false
}
}
implicit def ===(obj:Any):Boolean = {
obj match {
case x:P => x.name.contentEquals(this.name)
case _ => false
}
}
}
// Exiting paste mode, now interpreting.
警告:有一个功能警告;重新运行-feature以获取详细信息 定义了类P
val ps = Array(P("alice"), P("bob"), P("charly"), P("bob"))
ps: Array[P] = Array(P(alice), P(bob), P(charly), P(bob))
sc.parallelize(ps).map(x=> (x,1)).reduceByKey((x,y) => x+y).collect
res9: Array[(P, Int)] = Array((P(alice),1), (P(charly),1), (P(bob),1), (P(bob),1)) `