Created
June 23, 2016 07:08
-
-
Save akonopko/af48c91170fa8f344bcb63aa31b82037 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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