Database Reference
In-Depth Information
<goals>
<goal>
schema
</goal>
</goals>
<configuration>
<includes>
<include>
StringPair.avsc
</include>
</includes>
<stringType>
String
</stringType>
<sourceDirectory>
src/main/resources
</sourceDirectory>
<outputDirectory>
${project.build.directory}/
generated-sources/java
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
As an alternative to Maven, you can use Avro's Ant task,
to generate Java code for a schema.
In the code for serializing and deserializing, instead of a
GenericRecord
we construct
a
StringPair
instance, which we write to the stream using a
Speci-
ficDatumWriter
and read back using a
SpecificDatumReader
:
StringPair datum
=
new
StringPair
();
datum
.
setLeft
(
"L"
);
datum
.
setRight
(
"R"
);
ByteArrayOutputStream out
=
new
ByteArrayOutputStream
();
DatumWriter
<
StringPair
>
writer
=
new
SpecificDatumWriter
<
StringPair
>(
StringPair
.
class
);
Encoder encoder
=
EncoderFactory
.
get
().
binaryEncoder
(
out
,
null
);
writer
.
write
(
datum
,
encoder
);
encoder
.
flush
();
out
.
close
();
DatumReader
<
StringPair
>
reader
=
new
SpecificDatumReader
<
StringPair
>(
StringPair
.
class
);
Decoder decoder
=
DecoderFactory
.
get
().
binaryDecoder
(
out
.
toByteArray
(),
null
);