5

I have a flat file that looks like as mentioned below.

id,name,desg,tdate
1,Alex,Business Manager,2016-01-01

I am using the Spark Context to read this file as follows.

val myFile = sc.textFile("file.txt")

I want to generate a Spark DataFrame from this file and I am using the following code to do so.

case class Record(id: Int, name: String,desg:String,tdate:String)

val myFile1 = myFile.map(x=>x.split(",")).map {
  case Array(id, name,desg,tdate) => Record(id.toInt, name,desg,tdate)
} 

myFile1.toDF()

This is giving me a DataFrame with id as int and rest of the columns as String.

I want the last column, tdate, to be casted to date type.

How can I do that?

1 Answer 1

8

You just need to convert the String to a java.sql.Date object. Then, your code can simply become:

import java.sql.Date
case class Record(id: Int, name: String,desg:String,tdate:Date)

val myFile1 = myFile.map(x=>x.split(",")).map {
  case Array(id, name,desg,tdate) => Record(id.toInt, name,desg,Date.valueOf(tdate))
} 

myFile1.toDF()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mark for another prompt reply! It worked for me and this time I got the chance to accept your answer as well :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.