Created
February 27, 2017 14:31
-
-
Save debop/a687c32e85d7fc2fc42e57b1ee32e72d to your computer and use it in GitHub Desktop.
AvroSerializer
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
open class AvroSerializer @JvmOverloads constructor(val codecFactory: CodecFactory = CodecFactory.snappyCodec()) { | |
/** | |
* Avro instance 를 직렬화하여 byte array 로 변환합니다. | |
*/ | |
fun <T : SpecificRecordBase> writeAvroObject(graph: T?): ByteArray { | |
return graph?.let { | |
val sdw = SpecificDatumWriter<T>(graph.schema) | |
val dfw = DataFileWriter(sdw).setCodec(codecFactory) | |
ByteArrayOutputStream() use { bos -> | |
dfw.create(graph.schema, bos) | |
dfw.append(graph) | |
dfw.flush() | |
bos.toByteArray() | |
} | |
} ?: ByteArray(0) | |
} | |
/** | |
* byte array 를 읽어 avro instance 로 빌드합니다. | |
*/ | |
@Suppress("UNCHECKED_CAST") | |
fun <T : SpecificRecordBase> readAvroObject(avroBytes: ByteArray?, clazz: Class<T>): T? { | |
return avroBytes?.let { | |
val sdr = SpecificDatumReader(clazz) | |
SeekableByteArrayInput(avroBytes) use { sin -> | |
val dfr = DataFileReader(sin, sdr) | |
if (dfr.hasNext()) dfr.next() else null | |
} | |
} | |
} | |
/** | |
* avro instance collection 을 byte array 로 변환합니다. | |
*/ | |
fun <T : SpecificRecordBase> writeAvroList(graphs: List<T>, clazz: Class<T>): ByteArray { | |
// val gclazz = getGenericParameterTypes<List<T>>(graphs) | |
val schema = SpecificData.get().getSchema(clazz) | |
val sdw: SpecificDatumWriter<T> = SpecificDatumWriter(schema) | |
val dfw: DataFileWriter<T> = DataFileWriter(sdw).setCodec(codecFactory) | |
val bos = ByteArrayOutputStream() | |
try { | |
dfw.create(schema, bos) | |
graphs.forEach { dfw.append(it) } | |
dfw.flush() | |
return bos.toByteArray() | |
} finally { | |
bos.close() | |
} | |
} | |
/** | |
* byte array 를 읽어 avro instance collection 을 빌드합니다. | |
*/ | |
fun <T : SpecificRecordBase> readAvroList(avroBytes: ByteArray?, clazz: Class<T>): List<T> { | |
val results = mutableListOf<T>() | |
avroBytes?.let { | |
val sdr = SpecificDatumReader(clazz) | |
SeekableByteArrayInput(avroBytes) use { sin -> | |
val dfr = DataFileReader(sin, sdr) | |
while (dfr.hasNext()) { | |
results.add(dfr.next()) | |
} | |
} | |
} | |
return results | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment