I'm newbie in Spark and I want to update the internal state of my RDD's elements with rdd.foreach
method, but it doesn't work. Here is my code example:
class Test extends Serializable{
var foo = 0.0
var bar = 0.0
def updateFooBar() = {
foo = Math.random()
bar = Math.random()
}
}
var testList = Array.fill(5)(new Test())
var testRDD = sc.parallelize(testList)
testRDD.foreach{ x => x.updateFooBar() }
testRDD.collect().foreach { x=> println(x.foo+"~"+x.bar) }
and the result is:
0.0~0.0
0.0~0.0
0.0~0.0
0.0~0.0
0.0~0.0