neoguy neoguy - 22 days ago 6
Scala Question

Unable to append RDD to a list in scala

Hi following i am unable to append values to my List in Scala following is my code:

var cumFrequency =List[Any]
updatedRdd.foreachRDD( rdd => {
for( item <- rdd.collect()) {
val newRDD = (item._1,item._2.toString.toDouble/newc)
cumFrequency +: newRDD
}

})


I am getting error at
cumFrequency +: newRDD
of can not resolve symbol. i am not sure why its giving that error when i want to append
newRDD
to list
cumFrequency
. am i writing in wrong syntax?

Answer

Use :+ not +:

Below is the corrected line

cumFrequency :+ newRDD

Initialise you cumFrequency using List.empty[Any] or List[Any]()

var cumFrequency = List[Any]()

or

var cumFrequency =List.empty[Any]

In order to append to list use ::: or ++ or :+

In case of List try this

cumFrequency ::: List(newRDD)

or

cumFrequency ++ List(newRDD)

or if you do not care about order then

newRDD :: cumFrequency