Reputation: 5870
I found when I put broadcast variable into a function, then the function can not get the values instead of a null pointer, the code as following:
def main(){
val serviceSet=Set("abc","ccc")
broadServiceSet = sparkContext.broadcast(serviceSet)
var keyRDD=esRdd.map(tp=>(map2Key(broadServiceSet.value),tp._1))
}
def map2Key(broadSet:Set[String]):String={
broadSet.contains("testName")//here threw NullPointerException
}
Upvotes: 0
Views: 496
Reputation:
From your code everything is good, it should not throw NullPointerException. The broadcast variables can be passed to function or other classes very well. I guess you may defined your broadcast variable as a Object field. If so, it would throw Exception, because the JVM compiler will override you did in main function instead of using the initial value which was previously defined as a class field.
Upvotes: 1