我试图在Spark数据框的单列中替换":" -> "_"的所有实例。我正在尝试这样做:
val url_cleaner = (s:String) => {
s.replaceAll(":","_")
}
val url_cleaner_udf = udf(url_cleaner)
val df = old_df.withColumn("newCol", url_cleaner_udf(old_df("oldCol")) )
但是我一直得到错误:
SparkException: Job aborted due to stage failure: Task 0 in stage 25.0 failed 4 times, most recent failure: Lost task 0.3 in stage 25.0 (TID 692, ip-10-81-194-29.ec2.internal): java.lang.NullPointerException
我在udf哪里出错了?
这一列可能有一些空。
试题:
val urlCleaner = (s:String) => {
if (s == null) null else s.replaceAll(":","_")
}
您也可以使用regexp_replace(col("newCol"), ":", "_")
代替自己的函数