简体   繁体   中英

Scala enum - Java.lang.UnsupportedOperationException

I am getting

java.lang.UnsupportedOperationException: Schema for type Range.Value is not supported.

Appreciate any pointers on this

object Range extends Enumeration {
  type Range = Value

  val RangeMedium = Value("Range Medium")
  val RangeHigh = Value("Range Higher")
  val RangeNotEnough = Value("Range Not enough")
  val NotApplicable = Value("Not Applicable")

}



val getRange = udf((p1: Double, p2: Double) => {
    if (p1 >= 5 && p1 < 10 && p2 >= 1) {
      Some(Range.RangeMedium)
    }
    else if (p1 >= 10 && p2 >= 1) {
      Some(Range.RangeHigh)
    }
    else {
      Some(Range.NotApplicable)
    }
  })

ds = Seq(9,10).toDF("p1","p2")

ds.withColumn("level",getRange($"p1",$"p2")).show()

If you're returning a string from the UDF, you can try casting the enum value to a string using .toString :

object Range extends Enumeration {
  type Range = Value

  val RangeMedium = Value("Range Medium")
  val RangeHigh = Value("Range Higher")
  val RangeNotEnough = Value("Range Not enough")
  val NotApplicable = Value("Not Applicable")
}

val getRange = udf((p1: Double, p2: Double) => {
    if (p1 >= 5 && p1 < 10 && p2 >= 1) {
      Range.RangeMedium.toString
    }
    else if (p1 >= 10 && p2 >= 1) {
      Range.RangeHigh.toString
    }
    else {
      Range.NotApplicable.toString
    }
})

val ds = Seq((9,10)).toDF("p1","p2")

ds.withColumn("level",getRange($"p1",$"p2")).show()
+---+---+------------+
| p1| p2|       level|
+---+---+------------+
|  9| 10|Range Medium|
+---+---+------------+

Having said that, this kind of operation is also possible using when statements instead of a UDF, which should be more performant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM