I have a dataframe with one DateTime column and many other columns.
All I wanted to do is parse this DateTime column value and check if the format is \"yyyy-MM
You can use filter()
to get the valid/invalid
records in dataframe. This code can be improvable with scala point of view.
val DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"
def validateDf(row: Row): Boolean = try {
//assume row.getString(1) with give Datetime string
java.time.LocalDateTime.parse(row.getString(1), java.time.format.DateTimeFormatter.ofPattern(DATE_TIME_FORMAT))
true
} catch {
case ex: java.time.format.DateTimeParseException => {
// Handle exception if you want
false
}
}
val session = SparkSession.builder
.appName("Validate Dataframe")
.getOrCreate
val df = session. .... //Read from any datasource
import session.implicits._ //implicits provide except() on df
val validDf = df.filter(validateDf(_))
val inValidDf = df.except(validDf)