Skip to content

Instantly share code, notes, and snippets.

@Kornel
Created November 22, 2017 07:34
Show Gist options
  • Save Kornel/f90e92e5b53b5f7bf3e3dbcc9f2b3dd5 to your computer and use it in GitHub Desktop.
Save Kornel/f90e92e5b53b5f7bf3e3dbcc9f2b3dd5 to your computer and use it in GitHub Desktop.
enum spark UDT
package org.apache.spark.sql.types
import xxx.Condition
import xxx.Department
class ConditionUDT extends UserDefinedType[Condition.Value] {
override def sqlType: DataType = IntegerType
override def serialize(obj: Condition.Value): Int = obj.id
override def deserialize(datum: Any): Condition.Value = Condition(datum.asInstanceOf[Int])
override def userClass: Class[Condition.Value] = classOf[Condition.Value]
}
object EnumUDTRegister {
def register(): Unit = {
val clazz = this.getClass.getClassLoader.loadClass("org.apache.spark.sql.types.UDTRegistration")
val m = clazz.getMethods.filter(_.getName == "register").head
m.setAccessible(true)
m.invoke(clazz, classOf[Condition.Value].getCanonicalName, "org.apache.spark.sql.types.ConditionUDT")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment