java.lang.ClassCastException: java.sql.Date cannot be cast to java.lang.String

Hi Sir,

Please help me on the below error: in spark 2.1.0 (scala-2.11)

java.lang.ClassCastException: java.sql.Date cannot be cast to java.lang.String
at org.apache.spark.sql.Row$class.getString(Row.scala)
at org.apache.spark.sql.catalyst.expressions.GenericRow.getString(rows.scala)

import java.text.SimpleDateFormat
val someRDD =RDD.groupByKey().mapPartitions {
(iterator) =>
{
val dataFormatter = new SimpleDateFormat(“yyyy-MM-dd”)
myList.map(ip => Row(ip._1.getInt(0),ip._1.getLong(1), ip._1.getInt(2),
new java.sql.Date(dataFormatter.parse(ip._1.getString(7)).getTime), new java.sql.Date(dataFormatter.parse(ip._1.getString(8)).getTime),
error is showing in the above 2 lines.

I have tried the different approach with the following ways.

	import java.sql.Date
	
    Date.valueOf(ip._1.getString(7)).getTime,
     Date.valueOf(ip._1.getString(8)).getTime,

Still I’m getting the same error saying "Caused by: java.lang.ClassCastException: java.sql.Date cannot be cast to java.lang.String

How did you know I would be male? Good guess though!

It looks like you are trying to get a value out of a Row as a String and parse it to a java.sql.Date, but the thing in the Row is already a java.sql.Date instead of a String. Try using the getDate or getAs[java.sql.Date] methods instead.

Thanks Jasper.