I need to extract and transform from a big dataset some information which will be later consumed by other dataset.
Since the information to be consumed is always the same, and since it can be stored in a pair-value fashion, I was considering to just save this information in a look-at map which will be consumed by a udf, so I avoid several calls to the big dataset.
The problem is I am getting the following error:
org.apache.spark.SparkException: Task not serializable
Is there any way to make the map serializable?
In case it is not possible, is there another way to store information in a look-at object in Spark?
Here is my code:
val cityTimeZone: scala.collection.immutable.Map[String,Double] = Map("CEB" -> 8.0, "LGW" -> 0.0, "CPT" -> 2.0
, "MUC" -> 1.0, "SGN" -> 7.0, "BNE" -> 10.0, "DME" -> 3.0, "FJR" -> 4.0, "BAH" -> 3.0, "ARN" -> 1.0, "FCO" -> 1.0, "DUS" -> 1.0, "MRU" -> 4.0, "JFK" -> -5.0, "GLA" -> 0.0)
def getLocalHour = udf ((city:String, timeutc:Int) => {
val timeOffset = cityTimeZone(city)
val localtime = if((timeutc+timeOffset)%24 >= 0)(timeutc+timeOffset)%24 else ((timeutc+timeOffset)%24)*(-1)
localtime
})
//$"dateutc" is a timestamp column like this: 2017-03-01 03:45:00 and $"city" a 3 letters code in capitals, like those in the map above
val newDF = DF
.select("dateutc","city")
.withColumn("utchour", hour($"dateutc"))
.withColumn("localhour", getLocalHour($"city", $"utchour"))
display(newDF)