Below is the UDF to convert multivalued column into map.
def convertToMapFn (c: String): Map[String,String] = {
val str = Option(c).getOrElse(return Map[String, String]())
val arr = str.split(",")
val l = arr.toList
val regexPattern = ".*(=).*".r
s"$c".toString match {
case regexPattern(a) => l.map(x => x.split("=")).map(a => {if(a.size==2) (a(0).toString -> a(1).toString) else "ip_adr" -> a(0).toString} ).toMap
case "null" => Map[String, String]()
}
}
val convertToMapUDF = udf(convertToMapFn _)
I am able to display the data, but while trying to insert the data into Delta table, I am getting the below error.
Caused by: org.apache.spark.SparkException: Job aborted due to stage failure: Task 9 in stage 97.0 failed 4 times, most recent failure: Lost task 9.3 in stage 97.0 (TID 2561, 10.73.244.39, executor 5): org.apache.spark.SparkException: Failed to execute user defined function($read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$Lambda$2326/1884779796: (string) => map<string,string>)
Caused by: scala.MatchError: a8:9f:e (of class java.lang.String)
at line396de0100d5344c9994f63f7de7884fe49.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw.convertToMapFn
Caused by: org.apache.spark.SparkException: Failed to execute user defined function($read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$Lambda$2326/1884779796: (string) => map<string,string
Could someone pls let me know how to fix this. Thank you
CodePudding user response:
You can see in the error message that you have a MatchError
. This happens when you don't account for all possible match cases. A basic fix is to change case "null" =>
to case _ =>
which will match anything that the regex doesn't.
Other matters:
s"$c".toString
is equivalent to writingc
in this case.- I think you mean to match on
str
and notc