neoguy neoguy - 1 year ago 88
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
to list
. am i writing in wrong syntax?

Answer Source

Use :+ not +:

Below is the corrected line

cumFrequency :+ newRDD

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

var cumFrequency = List[Any]()


var cumFrequency =List.empty[Any]

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

In case of List try this

cumFrequency ::: List(newRDD)


cumFrequency ++ List(newRDD)

or if you do not care about order then

newRDD :: cumFrequency 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download