Skip to content

Commit bb8a679

Browse files
author
spandey
committed
BAEL-1747: apache avro
1 parent 2022b7f commit bb8a679

10 files changed

Lines changed: 1137 additions & 0 deletions

File tree

apache-avro/pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>apache-avro-tutorial</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<compiler-plugin.version>3.5</compiler-plugin.version>
13+
<avro.version>1.8.2</avro.version>
14+
<java.version>1.8</java.version>
15+
<slf4j.version>1.7.25</slf4j.version>
16+
</properties>
17+
18+
<parent>
19+
<groupId>com.baeldung</groupId>
20+
<artifactId>parent-modules</artifactId>
21+
<version>1.0.0-SNAPSHOT</version>
22+
</parent>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>junit</groupId>
27+
<artifactId>junit</artifactId>
28+
<version>4.10</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-simple</artifactId>
34+
<version>${slf4j.version}</version>
35+
<scope>compile</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.apache.avro</groupId>
39+
<artifactId>avro</artifactId>
40+
<version>${avro.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.apache.avro</groupId>
44+
<artifactId>avro-compiler</artifactId>
45+
<version>${avro.version}</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.apache.avro</groupId>
50+
<artifactId>avro-maven-plugin</artifactId>
51+
<version>${avro.version}</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-compiler-plugin</artifactId>
60+
<version>${compiler-plugin.version}</version>
61+
<configuration>
62+
<source>${java.version}</source>
63+
<target>${java.version}</target>
64+
</configuration>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.apache.avro</groupId>
68+
<artifactId>avro-maven-plugin</artifactId>
69+
<version>${avro.version}</version>
70+
<executions>
71+
<execution>
72+
<id>schemas</id>
73+
<phase>generate-sources</phase>
74+
<goals>
75+
<goal>schema</goal>
76+
<goal>protocol</goal>
77+
<goal>idl-protocol</goal>
78+
</goals>
79+
<configuration>
80+
<sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
81+
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
82+
</configuration>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.avro.util;
2+
3+
import org.apache.avro.Schema;
4+
import org.apache.avro.compiler.specific.SpecificCompiler;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
9+
public class AvroClassGenerator {
10+
public void generateAvroClasses() throws IOException {
11+
SpecificCompiler compiler = new SpecificCompiler(new Schema.Parser().parse(new File("src/main/resources/avroHttpRequest-schema.avsc")));
12+
compiler.compileToDestination(new File("src/main/resources"), new File("src/main/java"));
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.avro.util;
2+
3+
4+
import org.apache.avro.Schema;
5+
import org.apache.avro.SchemaBuilder;
6+
7+
public class AvroSchemaBuilder {
8+
9+
public Schema createAvroHttpRequestSchema(){
10+
11+
Schema clientIdentifier = SchemaBuilder.record("ClientIdentifier").namespace("com.baeldung.avro.model")
12+
.fields().requiredString("hostName").requiredString("ipAddress").endRecord();
13+
14+
Schema avroHttpRequest = SchemaBuilder.record("AvroHttpRequest").namespace("com.baeldung.avro.model").fields()
15+
.requiredLong("requestTime")
16+
.name("clientIdentifier").type(clientIdentifier).noDefault()
17+
.name("employeeNames").type().array().items().stringType().arrayDefault(null)
18+
.name("active").type().enumeration("Active").symbols("YES", "NO").noDefault()
19+
.endRecord();
20+
return avroHttpRequest;
21+
}
22+
}
23+
24+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Autogenerated by Avro
3+
*
4+
* DO NOT EDIT DIRECTLY
5+
*/
6+
package com.baeldung.avro.util.model;
7+
@SuppressWarnings("all")
8+
@org.apache.avro.specific.AvroGenerated
9+
public enum Active {
10+
YES, NO ;
11+
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"enum\",\"name\":\"Active\",\"namespace\":\"com.baeldung.avro.model\",\"symbols\":[\"YES\",\"NO\"]}");
12+
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
13+
}

0 commit comments

Comments
 (0)