Created
May 4, 2014 23:22
-
-
Save hammer/e4be43c7233924ade855 to your computer and use it in GitHub Desktop.
Demonstration of problems with chill-avro (twitter/chill #183)
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
import com.twitter.bijection.avro.{GenericAvroCodec, GenericAvroCodecs} | |
import com.twitter.bijection.Injection | |
import com.twitter.chill._ | |
import org.apache.avro.generic.{GenericRecord, GenericRecordBuilder} | |
import org.apache.avro.SchemaBuilder | |
import scala.util.{Failure, Success} | |
object HelloKryo { | |
def main(args: Array[String]) { | |
// Build a schema | |
val schema = SchemaBuilder | |
.record("person") | |
.fields | |
.name("name").`type`().stringType().noDefault() | |
.name("ID").`type`().intType().noDefault() | |
.endRecord | |
// Build an object conforming to the schema | |
val user1 = new GenericRecordBuilder(schema) | |
.set("name", "Jeff") | |
.set("ID", 1) | |
.build | |
// Round trip with Twitter Bijection, works correctly | |
implicit val genericInjection = GenericAvroCodecs[GenericRecord](schema) | |
val bijectionBytes = Injection[GenericRecord, Array[Byte]](user1) | |
val tryDecodeBijection = Injection.invert[GenericRecord, Array[Byte]](bijectionBytes) | |
tryDecodeBijection match { | |
case Success(userBijection) => println("Avro record read from Bijection bytes: " + userBijection) | |
case Failure(_) => println("Failed to read Avro record from Bijection bytes") | |
} | |
// Round trip with Twitter Chill: first try | |
// Doesn't work in a weird way, deserializes as {"name": "Jeff", "ID": "Jeff"} | |
val kryoBytes: Array[Byte] = KryoInjection(user1) | |
val tryDecodeKryo: scala.util.Try[Any] = KryoInjection.invert(kryoBytes) | |
tryDecodeKryo match { | |
case Success(userKryo) => println("Avro record read from Kryo bytes: " + userKryo) | |
case Failure(_) => println("Failed to read Avro record from Kryo bytes") | |
} | |
// Round trip with Twitter Chill: second try | |
// Doesn't work in same weird way, deserializes as {"name": "Jeff", "ID": "Jeff"} | |
val inj = new GenericAvroCodec[GenericRecord](schema) | |
val injSer = InjectiveSerializer.asKryo(inj) | |
val inst = { () => (new ScalaKryoInstantiator).newKryo.forClass(injSer) } | |
val kryo = KryoPool.withByteArrayOutputStream(1, inst) | |
val kryoAvroBytes = kryo.toBytesWithClass(user1) | |
val userKryoAvro = kryo.fromBytes(kryoAvroBytes) | |
println("Avro record read from Kryo bytes with schema: " + userKryoAvro) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment