Skip to content

Instantly share code, notes, and snippets.

@rjurney

rjurney/stdout

Created Jun 6, 2014
Embed
What would you like to do?
Results of attempt to use https://gist.github.com/massie/7224868
<console>:43: error: type mismatch;
found : Class[T]
required: org.apache.avro.Schema
val reader = new GenericDatumReader[T](classManifest[T].erasure.asInstanceOf[Class[T]])
^
<console>:44: error: type mismatch;
found : Class[T]
required: org.apache.avro.Schema
val writer = new GenericDatumWriter[T](classManifest[T].erasure.asInstanceOf[Class[T]])
import com.esotericsoftware.kryo.{Kryo, Serializer}
import com.esotericsoftware.kryo.io.{Input, Output}
import org.apache.avro.io.{BinaryDecoder, DecoderFactory, BinaryEncoder, EncoderFactory}
import org.apache.spark.serializer.KryoRegistrator
class AvroSerializer[T <: GenericRecord : ClassManifest] extends Serializer[T] {
val reader = new GenericDatumReader[T](classManifest[T].erasure.asInstanceOf[Class[T]])
val writer = new GenericDatumWriter[T](classManifest[T].erasure.asInstanceOf[Class[T]])
var encoder = null.asInstanceOf[BinaryEncoder]
var decoder = null.asInstanceOf[BinaryDecoder]
setAcceptsNull(false)
def write(kryo: Kryo, output: Output, record: T) = {
encoder = EncoderFactory.get().directBinaryEncoder(output, encoder)
writer.write(record, encoder)
}
def read(kryo: Kryo, input: Input, klazz: Class[T]): T = this.synchronized {
decoder = DecoderFactory.get().directBinaryDecoder(input, decoder)
reader.read(null.asInstanceOf[T], decoder)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.