Skip to content

Instantly share code, notes, and snippets.

@akonopko
Created June 23, 2016 07:08
Show Gist options
  • Save akonopko/af48c91170fa8f344bcb63aa31b82037 to your computer and use it in GitHub Desktop.
Save akonopko/af48c91170fa8f344bcb63aa31b82037 to your computer and use it in GitHub Desktop.
package com.memsql.spark.connector.dataframe
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.UTF8String
@SQLUserDefinedType(udt = classOf[UTF8JsonType])
class UTF8JsonValue(val value: String) extends Serializable with MemSQLCustomType {
override def toString: String = value
}
/**
* Spark SQL [[org.apache.spark.sql.types.UserDefinedType]] for MemSQL's `JSON` column type.
*/
class UTF8JsonType private() extends UserDefinedType[UTF8JsonValue] {
override def sqlType: DataType = StringType
override def serialize(obj: Any): Any = {
obj match {
case x: UTF8JsonValue => UTF8String.fromString(x.value)
case x: String => UTF8String.fromString(x)
case x: UTF8String => x
}
}
override def deserialize(datum: Any): UTF8JsonValue = {
datum match {
case x: UTF8String => new UTF8JsonValue(x.toString)
case x: String => new UTF8JsonValue(x)
}
}
override def userClass: Class[UTF8JsonValue] = classOf[UTF8JsonValue]
override def asNullable: UTF8JsonType = this
override def typeName: String = "json"
}
case object UTF8JsonType extends UTF8JsonType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment