Created
May 16, 2018 14:10
-
-
Save timvw/36758863d77670c821b89c3d0f715b69 to your computer and use it in GitHub Desktop.
avro encode/decode single record
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
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