Search code examples
scalaapache-sparkudf

No typeTag exception when trying to pass Map as parameter to Udf


I have been trying to write an UDF. I am receiving an error when I am trying to build the package with SBT.

 val getUiPropx = udf((prop_map:Map[String, String],prop:String,sub_prop:String,alt_prop:String) => {
if (prop_map.contains(prop)) {
  val ui_element_map = jsonStrToMap(prop_map(prop))
  if (ui_element_map.contains(sub_prop)) ui_element_map(sub_prop).toString() else "-1"
}
else {
  if (prop_map.contains(alt_prop)) prop_map(alt_prop).toString() else "-1"
} })

The error I receive is -

scala:87: No TypeTag available for Map[String,String]  [error] val getUiPropx = udf((prop_map:Map[String, String],prop:String,sub_prop:String,alt_prop:String) => {

The thing is I am not even invoking the udf yet but I still receive the error. Could someone please explain the error?

Edit -

As suggested in the answer below and in the thread - Pass array as an UDF parameter in Spark SQL I modified the udf call to -

  def getUiPropx(prop_map:Map[String, String],prop:String,sub_prop:String,alt_prop:String) = {
udf(() => {
  if (prop_map.contains(prop)) {
    val ui_element_map = jsonStrToMap(prop_map(prop))
    if (ui_element_map.contains(sub_prop)) ui_element_map(sub_prop).toString() else "-1"
  }
  else {
    if (prop_map.contains(alt_prop)) prop_map(alt_prop).toString() else "-1"
  }
})}

and I invoke it -

val trnUiEventDf = hiveDf..withColumn("ui_element_id",getUiPropx(myMap,"ui_element","id","ui_element_id")(col("ep_map")))

I still receive an error in the runtime saying Unit is not allowed in dataframes. Is it because I have not specified any return type the function?


Solution

  • First parameter in the udf() function should have some type which is compatible with the sql type. It actually represents the type of the field which you are sending into that field. For Map[String,String] there is no type associated in sql side.