我的 Scala 代碼是這樣的。
def getFileName(fileName: String, postfixList: List[String]): String = {
if (...) {
return fileName postfixList(0)
}
return fileName postfixList(1)
}
val getFileNameUdf = udf(getFileName(_: String, _: List[String]): String)
def main(args: Array[String]): Unit = {
val postfixList = List("a", "b")
val rawFsRecordDF = sparkSession.read.option("delimiter", "\t").schema(fsImageSchema)
.withColumn("fileName", getFileNameUdf(col("name"), lit(postfixList)))
}
對于每個列值,我想附加某個后綴。
運行這段代碼,我得到
診斷:用戶類拋出例外:java.lang.RuntimeException:不支持的文字型別類 scala.collection.immutable.Nil$ List()
我應該如何將串列傳遞給 UDF 函式?
任何評論或鏈接將不勝感激!
uj5u.com熱心網友回復:
lit適用于基本型別。對于引數化型別,您應該使用typedLit.
val rawFsRecordDF = sparkSession.read.option("delimiter", "\t").schema(fsImageSchema)
.withColumn("fileName", getFileNameUdf(col("name"), typedLit(postfixList)))
應該管用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311429.html
上一篇:使用懶惰評估的perl腳本的解釋
