2017-02-08 16 views
0

我正在使用Spark從hbase讀取數據,並且我在數據框中有日期列,並且很少有數據字段已被損壞。像10-20176-7之類的東西。如何檢查這些數據並用一些默認值替換它們在我進一步處理之前。驗證scala中的數據框中的日期列?

謝謝。

+2

可以顯示到目前爲止你已經嘗試了什麼?給我們一些代碼開始... –

+0

你可以檢查現有的答案[驗證日期](http://stackoverflow.com/a/40510441/647053),然後你可以用默認值替換無效的日期。 –

+0

@RamGhadiyaram,我實現了上面的邏輯,並在數據框中獲得零記錄,雖然我有有效日期的記錄。 – GSR

回答

0

我堆棧跟蹤錯誤,下面是錯誤。

Exception in thread "main" java.time.format.DateTimeParseException: 
Text '20140218' could not be parsed: 
Unable to obtain LocalDateTime from TemporalAccessor: 
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed 
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918) 
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853) 
at java.time.LocalDateTime.parse(LocalDateTime.java:492) 

所以我用LocalDate而不是LocalDateTime來解決問題。以下是使用的示例代碼。

def validateDfsdate(row: Row): Boolean = try { 

val a = java.time.LocalDate.parse(row.getString(40), java.time.format.DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)) 

true 

} catch { 
case ex: java.time.format.DateTimeParseException => { 
    println("Exception : " + ex) 
    false 
} 

}