简体   繁体   中英

org.apache.spark.SparkException: Task not serializable for date function

I implemented Kryo serialization in spark 1.6.2 in scala. I got "org.apache.spark.SparkException: Task not serializable" for below getdate method.

I am using below getdate function in map transformation to get date.

def getdate(s: String,format_ts:String): Option[Date] = s match {
case "" => null
case _ =>
  val format = new SimpleDateFormat(format_ts)

  Try(new Date(format.parse(s).getTime)).toOption


 }

But when I insert null inplace of date column, it is working fine.

Can someone help me what are the classes need to be part of kryo serialization?.Thanks

I'm not much familiar with Kryo serialization in spark. But according to my experiance SimpleDateFormat is not serializable with spark. SimpleDateFormat class Implemented Serializable in java. But it is not serializable when using with spark( When it parse to the Executor program of hadoop, the class can't be serialized.) I got the same error. You can use LocalDate instead of using SimpleDateFormat for your task. For me LocalDate worked fine in many cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM