Skip to content

Instantly share code, notes, and snippets.

@timvw
Created May 16, 2018 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timvw/36758863d77670c821b89c3d0f715b69 to your computer and use it in GitHub Desktop.
Save timvw/36758863d77670c821b89c3d0f715b69 to your computer and use it in GitHub Desktop.
avro encode/decode single record
val entityMetadataSchema = SchemaBuilder
.record("entityMetadata")
.fields()
.name("relatedLookupRecords").`type`().array().items(Schema.create(Schema.Type.STRING)).noDefault()
.endRecord()
def serialize(x: GenericRecord, s: Schema): Array[Byte] = {
val baos = new ByteArrayOutputStream()
val fw = new DataFileWriter[GenericRecord](new GenericDatumWriter[GenericRecord]())
fw.create(s, baos)
fw.append(x)
fw.close()
baos.toByteArray
}
def deserialize(x: Array[Byte], s: Schema): GenericRecord = {
val sbai = new SeekableByteArrayInput(x)
val dr = new DataFileReader[GenericRecord](sbai, new GenericDatumReader[GenericRecord](s))
val record = dr.next()
dr.close()
record
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment