我試圖在我的函數中使用udf並在我的主函數中調用此函數。編譯時沒有錯誤。但是當我試圖運行這個應用程序時,它給出了錯誤:無法使用udf的行號初始化具有主類名稱的類。以下是代碼結構:無法初始化由udf引起的類火花
object shuffle {
create logger, conf, sparksession...
def main(): Unit = {
get configuration from file
val df = read in data
try {
val finaldf = my_function(df)
finaldf.write_back_as_parquet
} catch {
deal with exception
}
end
}
def my_function(arg : DataFrame): DataFrame = {
val DateFormat = new SimpleDateFormat("yyyy-MM-dd")
val dateConverter = udf((ts : String) => DateFormat.format(new Date(ts.toLong * 1000))) <----------error message point at this line
arg.withColumn("ts", dateConverter($"value"))
.withColumn("hh", lit(23)).withColumn("mm", lit(59))
}
}
我感到困惑。爲什麼我得到這個錯誤?是否有我在哪裏放udf的規則?
udf應該在被調用之前被定義。如果您使用您用來調用udf函數 –
的代碼更新您的文章將是有幫助的,但您仍未更新如何調用dateconverter udf函數代碼。並請更新完整的錯誤信息。 –