Question

I was looking for a Spark program that adds the elements of an existing Integer ArrayList.I went through all transformations and actions in apache spark but couldn't find a suitable one to just add the elements.

If someone could tell me how to write the code for the above ie add elements of an arraylist in spark , then it'll be great.

Thanks.

Was it helpful?

Solution

If you have a RDD[Int] as shown below:

val myRdd = sc.parallelize(Seq(1,2,3,4,5,6))

you could do the following to add the elements of the List:

myRdd.reduce(_+_)

res1: Int = 21

Or you could do the following as well:

myRdd.fold(0)(_+_)

res6: Int = 21

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top