I am using Spark to do employee record accumulation and for that I use Spark's accumulator. I am using Map[empId, emp] as accumulableCollection so that I can search employee by their ids. I have tried everything but it does not work. Can someone point if there is any logical issues with the way I am using accumulableCollection or Map is not supported. Following is my code
package demo
import org.apache.spark.{SparkContext, SparkConf, Logging}
import org.apache.spark.SparkContext._
import scala.collection.mutable
object MapAccuApp extends App with Logging {
case class Employee(id:String, name:String, dept:String)
val conf = new SparkConf().setAppName("Employees") setMaster ("local[4]")
val sc = new SparkContext(conf)
implicit def empMapToSet(empIdToEmp: mutable.Map[String, Employee]): mutable.MutableList[Employee] = {
empIdToEmp.foldLeft(mutable.MutableList[Employee]()) { (l, e) => l += e._2}
}
val empAccu = sc.accumulableCollection[mutable.Map[String, Employee], Employee](mutable.Map[String,Employee]())
val employees = List(
Employee("10001", "Tom", "Eng"),
Employee("10002", "Roger", "Sales"),
Employee("10003", "Rafael", "Sales"),
Employee("10004", "David", "Sales"),
Employee("10005", "Moore", "Sales"),
Employee("10006", "Dawn", "Sales"),
Employee("10007", "Stud", "Marketing"),
Employee("10008", "Brown", "QA")
)
System.out.println("employee count " + employees.size)
sc.parallelize(employees).foreach(e => {
empAccu += e
})
System.out.println("empAccumulator size " + empAccu.value.size)
}
Using
accumulableCollection
seems like overkill for your problem, as the following demonstrates:While this is poorly documented right now, the relevant test in the Spark codebase is quite illuminating.
Edit: It turns out that using
accumulableCollection
does have value: you don't need to define anAccumulableParam
and the following works. I'm leaving both solutions in case they're useful to people.Both solutions tested using Spark 1.0.2.