File tree Expand file tree Collapse file tree 4 files changed +121
-0
lines changed
main/java/com/baeldung/avro
test/java/com/baeldung/avro Expand file tree Collapse file tree 4 files changed +121
-0
lines changed Original file line number Diff line number Diff line change 4040 <artifactId >camel-jackson</artifactId >
4141 <version >${camel.version} </version >
4242 </dependency >
43+ <dependency >
44+ <groupId >org.apache.avro</groupId >
45+ <artifactId >avro</artifactId >
46+ <version >${apache.avro.version} </version >
47+ <exclusions >
48+ <exclusion >
49+ <groupId >com.fasterxml.jackson.core</groupId >
50+ <artifactId >jackson-core</artifactId >
51+ </exclusion >
52+ <exclusion >
53+ <groupId >com.fasterxml.jackson.core</groupId >
54+ <artifactId >jackson-databind</artifactId >
55+ </exclusion >
56+ <exclusion >
57+ <groupId >com.fasterxml.jackson.core</groupId >
58+ <artifactId >jackson-annotations</artifactId >
59+ </exclusion >
60+ </exclusions >
61+ </dependency >
4362 </dependencies >
4463
64+ <build >
65+ <plugins >
66+ <plugin >
67+ <groupId >org.apache.avro</groupId >
68+ <artifactId >avro-maven-plugin</artifactId >
69+ <version >${apache.avro.version} </version >
70+ <configuration >
71+ <sourceDirectory >${project.basedir} /src/main/java/com/baeldung/avro/</sourceDirectory >
72+ <outputDirectory >${project.basedir} /src/main/java/com/baeldung/avro/</outputDirectory >
73+ <stringType >String</stringType >
74+ </configuration >
75+ <executions >
76+ <execution >
77+ <phase >generate-sources</phase >
78+ <goals >
79+ <goal >schema</goal >
80+ </goals >
81+ </execution >
82+ </executions >
83+ </plugin >
84+ </plugins >
85+ </build >
86+
4587 <properties >
4688 <javax .validation.validation-api.version>2.0.1.Final</javax .validation.validation-api.version>
4789 <camel .version>4.4.1</camel .version>
90+ <apache .avro.version>1.11.3</apache .avro.version>
4891 </properties >
4992
5093</project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .avro ;
2+
3+ import generated .avro .Car ;
4+
5+ import org .apache .avro .file .DataFileReader ;
6+ import org .apache .avro .file .DataFileWriter ;
7+ import org .apache .avro .io .DatumReader ;
8+ import org .apache .avro .io .DatumWriter ;
9+ import org .apache .avro .specific .SpecificDatumReader ;
10+ import org .apache .avro .specific .SpecificDatumWriter ;
11+
12+ import java .io .File ;
13+ import java .io .IOException ;
14+
15+ public class SerializationDeserializationLogic {
16+
17+ static void serializeCar (Car car ) throws IOException {
18+ DatumWriter <Car > userDatumWriter = new SpecificDatumWriter (Car .class );
19+
20+ try (DataFileWriter <Car > dataFileWriter = new DataFileWriter (userDatumWriter )) {
21+ dataFileWriter .create (car .getSchema (), new File ("cars.avro" ));
22+ dataFileWriter .append (car );
23+ }
24+ }
25+
26+ static Car deserializeCar () throws IOException {
27+ DatumReader <Car > userDatumReader = new SpecificDatumReader (Car .class );
28+
29+ try (DataFileReader <Car > dataFileReader = new DataFileReader (new File ("cars.avro" ), userDatumReader )) {
30+ return dataFileReader .next ();
31+ }
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ {
2+ "namespace" : " generated.avro" ,
3+ "type" : " record" ,
4+ "name" : " Car" ,
5+ "fields" : [
6+ { "name" : " brand" ,
7+ "type" : " string" ,
8+ "default" : " Dacia"
9+ },
10+ { "name" : " number_of_doors" ,
11+ "type" : " int" ,
12+ "default" : 4
13+ },
14+ { "name" : " color" ,
15+ "type" : [" null" , " string" ],
16+ "default" : null
17+ }
18+ ]
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .avro ;
2+
3+ import generated .avro .Car ;
4+
5+ import org .junit .jupiter .api .Test ;
6+
7+ import java .io .IOException ;
8+
9+ import static org .junit .jupiter .api .Assertions .*;
10+
11+ public class AvroDefaultValuesUnitTest {
12+
13+ @ Test
14+ public void givenCarJsonSchema_whenCarIsSerialized_thenCarIsSuccessfullyDeserialized () throws IOException {
15+
16+ Car car = Car .newBuilder ()
17+ .build ();
18+
19+ SerializationDeserializationLogic .serializeCar (car );
20+ Car deserializedCar = SerializationDeserializationLogic .deserializeCar ();
21+
22+ assertEquals ("Dacia" , deserializedCar .getBrand ());
23+ assertEquals (4 , deserializedCar .getNumberOfDoors ());
24+ assertNull (deserializedCar .getColor ());
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments