From 73b323b9dcdbe0576fcb539d69384783c8bd86d8 Mon Sep 17 00:00:00 2001 From: rutkowskij Date: Fri, 9 Aug 2024 08:32:27 +0200 Subject: [PATCH] Add migration from AvroEncodeFormat.Binary example --- Migrating-from-v1.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Migrating-from-v1.md b/Migrating-from-v1.md index 88b4fd3..6d580a4 100644 --- a/Migrating-from-v1.md +++ b/Migrating-from-v1.md @@ -166,6 +166,21 @@ Files.newOutputStream(Path("/your/file.avro")).use { outputStream -> } ``` +## Reading previously written binary encoded (`AvroEncodeFormat.Binary`) files + +It was possible to use the `AvroEncodeFormat.Binary` format, which used [binary encoding](https://avro.apache.org/docs/current/spec.html#binary_encoding). The data did not have an embedded schema, so it had to be specified when reading. + +```kotlin +// Previously +Avro.default.openInputStream(serializer) { decodeFormat = AvroDecodeFormat.Binary(schema) } + .from(data).use { avroInputStream -> return avroInputStream.nextOrThrow() } + +// Now +val reader = GenericDatumReader(schema) +val decoder = DecoderFactory.get().binaryDecoder(ByteArrayInputStream(data), null) +Avro.decodeFromGenericData(schema, serializer, reader.read(null, decoder)) +``` + ## Writing a collection of Byte as BYTES or FIXED If you really want to encode a `BYTES` type, just use the `ByteArray` type or write your own `AvroSerializer` to control the schema and its serialization.