I'm trying to read a csv file from a local file system, create a dataframe from it, delete the file and return the dataframe. Yes, I have to delete it. Since everything is done lazily except for the deletion, the app fails since it can't find the file when the code gets executed.
def do_something() : DataFrame {
val file = File.createTempFile("query2Output", ".csv")
//some code which writes to the file
val df = sqlContext.read
.format("com.databricks.spark.csv")
.option("header", "true")
.option("mode", "DROPMALFORMED")
.load(file.getPath)
file.delete
df
}