From bb8a679dc0b287ecba1aa842daab07544f407a14 Mon Sep 17 00:00:00 2001 From: spandey Date: Wed, 11 Jul 2018 10:07:41 +0200 Subject: [PATCH] BAEL-1747: apache avro --- apache-avro/pom.xml | 88 ++++ .../avro/util/AvroClassGenerator.java | 14 + .../baeldung/avro/util/AvroSchemaBuilder.java | 24 + .../com/baeldung/avro/util/model/Active.java | 13 + .../avro/util/model/AvroHttpRequest.java | 491 ++++++++++++++++++ .../avro/util/model/ClientIdentifier.java | 308 +++++++++++ .../util/serealization/AvroDeSerealizer.java | 33 ++ .../util/serealization/AvroSerealizer.java | 44 ++ .../resources/avroHttpRequest-schema.avsc | 47 ++ .../AvroSerealizerDeSerealizerTest.java | 75 +++ 10 files changed, 1137 insertions(+) create mode 100644 apache-avro/pom.xml create mode 100644 apache-avro/src/main/java/com/baeldung/avro/util/AvroClassGenerator.java create mode 100644 apache-avro/src/main/java/com/baeldung/avro/util/AvroSchemaBuilder.java create mode 100644 apache-avro/src/main/java/com/baeldung/avro/util/model/Active.java create mode 100644 apache-avro/src/main/java/com/baeldung/avro/util/model/AvroHttpRequest.java create mode 100644 apache-avro/src/main/java/com/baeldung/avro/util/model/ClientIdentifier.java create mode 100644 apache-avro/src/main/java/com/baeldung/avro/util/serealization/AvroDeSerealizer.java create mode 100644 apache-avro/src/main/java/com/baeldung/avro/util/serealization/AvroSerealizer.java create mode 100644 apache-avro/src/main/resources/avroHttpRequest-schema.avsc create mode 100644 apache-avro/src/test/java/com/baeldung/avro/util/serealization/AvroSerealizerDeSerealizerTest.java diff --git a/apache-avro/pom.xml b/apache-avro/pom.xml new file mode 100644 index 0000000000..39da518269 --- /dev/null +++ b/apache-avro/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + com.baeldung + apache-avro-tutorial + 0.0.1-SNAPSHOT + + + UTF-8 + 3.5 + 1.8.2 + 1.8 + 1.7.25 + + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + junit + junit + 4.10 + test + + + org.slf4j + slf4j-simple + ${slf4j.version} + compile + + + org.apache.avro + avro + ${avro.version} + + + org.apache.avro + avro-compiler + ${avro.version} + + + + org.apache.avro + avro-maven-plugin + ${avro.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${compiler-plugin.version} + + ${java.version} + ${java.version} + + + + org.apache.avro + avro-maven-plugin + ${avro.version} + + + schemas + generate-sources + + schema + protocol + idl-protocol + + + ${project.basedir}/src/main/resources/ + ${project.basedir}/src/main/java/ + + + + + + + diff --git a/apache-avro/src/main/java/com/baeldung/avro/util/AvroClassGenerator.java b/apache-avro/src/main/java/com/baeldung/avro/util/AvroClassGenerator.java new file mode 100644 index 0000000000..718b62a752 --- /dev/null +++ b/apache-avro/src/main/java/com/baeldung/avro/util/AvroClassGenerator.java @@ -0,0 +1,14 @@ +package com.baeldung.avro.util; + +import org.apache.avro.Schema; +import org.apache.avro.compiler.specific.SpecificCompiler; + +import java.io.File; +import java.io.IOException; + +public class AvroClassGenerator { + public void generateAvroClasses() throws IOException { + SpecificCompiler compiler = new SpecificCompiler(new Schema.Parser().parse(new File("src/main/resources/avroHttpRequest-schema.avsc"))); + compiler.compileToDestination(new File("src/main/resources"), new File("src/main/java")); + } +} diff --git a/apache-avro/src/main/java/com/baeldung/avro/util/AvroSchemaBuilder.java b/apache-avro/src/main/java/com/baeldung/avro/util/AvroSchemaBuilder.java new file mode 100644 index 0000000000..4a1314cd00 --- /dev/null +++ b/apache-avro/src/main/java/com/baeldung/avro/util/AvroSchemaBuilder.java @@ -0,0 +1,24 @@ +package com.baeldung.avro.util; + + +import org.apache.avro.Schema; +import org.apache.avro.SchemaBuilder; + +public class AvroSchemaBuilder { + + public Schema createAvroHttpRequestSchema(){ + + Schema clientIdentifier = SchemaBuilder.record("ClientIdentifier").namespace("com.baeldung.avro.model") + .fields().requiredString("hostName").requiredString("ipAddress").endRecord(); + + Schema avroHttpRequest = SchemaBuilder.record("AvroHttpRequest").namespace("com.baeldung.avro.model").fields() + .requiredLong("requestTime") + .name("clientIdentifier").type(clientIdentifier).noDefault() + .name("employeeNames").type().array().items().stringType().arrayDefault(null) + .name("active").type().enumeration("Active").symbols("YES", "NO").noDefault() + .endRecord(); + return avroHttpRequest; + } +} + + diff --git a/apache-avro/src/main/java/com/baeldung/avro/util/model/Active.java b/apache-avro/src/main/java/com/baeldung/avro/util/model/Active.java new file mode 100644 index 0000000000..3ae0508394 --- /dev/null +++ b/apache-avro/src/main/java/com/baeldung/avro/util/model/Active.java @@ -0,0 +1,13 @@ +/** + * Autogenerated by Avro + * + * DO NOT EDIT DIRECTLY + */ +package com.baeldung.avro.util.model; +@SuppressWarnings("all") +@org.apache.avro.specific.AvroGenerated +public enum Active { + YES, NO ; + 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\"]}"); + public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; } +} diff --git a/apache-avro/src/main/java/com/baeldung/avro/util/model/AvroHttpRequest.java b/apache-avro/src/main/java/com/baeldung/avro/util/model/AvroHttpRequest.java new file mode 100644 index 0000000000..56b36050a5 --- /dev/null +++ b/apache-avro/src/main/java/com/baeldung/avro/util/model/AvroHttpRequest.java @@ -0,0 +1,491 @@ +/** + * Autogenerated by Avro + * + * DO NOT EDIT DIRECTLY + */ +package com.baeldung.avro.util.model; + +import org.apache.avro.specific.SpecificData; +import org.apache.avro.message.BinaryMessageEncoder; +import org.apache.avro.message.BinaryMessageDecoder; +import org.apache.avro.message.SchemaStore; + +@SuppressWarnings("all") +@org.apache.avro.specific.AvroGenerated +public class AvroHttpRequest extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord { + private static final long serialVersionUID = -8649010116827875312L; + public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"AvroHttpRequest\",\"namespace\":\"com.baeldung.avro.model\",\"fields\":[{\"name\":\"requestTime\",\"type\":\"long\"},{\"name\":\"clientIdentifier\",\"type\":{\"type\":\"record\",\"name\":\"ClientIdentifier\",\"fields\":[{\"name\":\"hostName\",\"type\":\"string\"},{\"name\":\"ipAddress\",\"type\":\"string\"}]}},{\"name\":\"employeeNames\",\"type\":{\"type\":\"array\",\"items\":\"string\"},\"default\":null},{\"name\":\"active\",\"type\":{\"type\":\"enum\",\"name\":\"Active\",\"symbols\":[\"YES\",\"NO\"]}}]}"); + public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; } + + private static SpecificData MODEL$ = new SpecificData(); + + private static final BinaryMessageEncoder ENCODER = + new BinaryMessageEncoder(MODEL$, SCHEMA$); + + private static final BinaryMessageDecoder DECODER = + new BinaryMessageDecoder(MODEL$, SCHEMA$); + + /** + * Return the BinaryMessageDecoder instance used by this class. + */ + public static BinaryMessageDecoder getDecoder() { + return DECODER; + } + + /** + * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}. + * @param resolver a {@link SchemaStore} used to find schemas by fingerprint + */ + public static BinaryMessageDecoder createDecoder(SchemaStore resolver) { + return new BinaryMessageDecoder(MODEL$, SCHEMA$, resolver); + } + + /** Serializes this AvroHttpRequest to a ByteBuffer. */ + public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException { + return ENCODER.encode(this); + } + + /** Deserializes a AvroHttpRequest from a ByteBuffer. */ + public static AvroHttpRequest fromByteBuffer( + java.nio.ByteBuffer b) throws java.io.IOException { + return DECODER.decode(b); + } + + @Deprecated public long requestTime; + @Deprecated public ClientIdentifier clientIdentifier; + @Deprecated public java.util.List employeeNames; + @Deprecated public Active active; + + /** + * Default constructor. Note that this does not initialize fields + * to their default values from the schema. If that is desired then + * one should use newBuilder(). + */ + public AvroHttpRequest() {} + + /** + * All-args constructor. + * @param requestTime The new value for requestTime + * @param clientIdentifier The new value for clientIdentifier + * @param employeeNames The new value for employeeNames + * @param active The new value for active + */ + public AvroHttpRequest(java.lang.Long requestTime, ClientIdentifier clientIdentifier, java.util.List employeeNames, Active active) { + this.requestTime = requestTime; + this.clientIdentifier = clientIdentifier; + this.employeeNames = employeeNames; + this.active = active; + } + + public org.apache.avro.Schema getSchema() { return SCHEMA$; } + // Used by DatumWriter. Applications should not call. + public java.lang.Object get(int field$) { + switch (field$) { + case 0: return requestTime; + case 1: return clientIdentifier; + case 2: return employeeNames; + case 3: return active; + default: throw new org.apache.avro.AvroRuntimeException("Bad index"); + } + } + + // Used by DatumReader. Applications should not call. + @SuppressWarnings(value="unchecked") + public void put(int field$, java.lang.Object value$) { + switch (field$) { + case 0: requestTime = (java.lang.Long)value$; break; + case 1: clientIdentifier = (ClientIdentifier)value$; break; + case 2: employeeNames = (java.util.List)value$; break; + case 3: active = (Active)value$; break; + default: throw new org.apache.avro.AvroRuntimeException("Bad index"); + } + } + + /** + * Gets the value of the 'requestTime' field. + * @return The value of the 'requestTime' field. + */ + public java.lang.Long getRequestTime() { + return requestTime; + } + + /** + * Sets the value of the 'requestTime' field. + * @param value the value to set. + */ + public void setRequestTime(java.lang.Long value) { + this.requestTime = value; + } + + /** + * Gets the value of the 'clientIdentifier' field. + * @return The value of the 'clientIdentifier' field. + */ + public ClientIdentifier getClientIdentifier() { + return clientIdentifier; + } + + /** + * Sets the value of the 'clientIdentifier' field. + * @param value the value to set. + */ + public void setClientIdentifier(ClientIdentifier value) { + this.clientIdentifier = value; + } + + /** + * Gets the value of the 'employeeNames' field. + * @return The value of the 'employeeNames' field. + */ + public java.util.List getEmployeeNames() { + return employeeNames; + } + + /** + * Sets the value of the 'employeeNames' field. + * @param value the value to set. + */ + public void setEmployeeNames(java.util.List value) { + this.employeeNames = value; + } + + /** + * Gets the value of the 'active' field. + * @return The value of the 'active' field. + */ + public Active getActive() { + return active; + } + + /** + * Sets the value of the 'active' field. + * @param value the value to set. + */ + public void setActive(Active value) { + this.active = value; + } + + /** + * Creates a new AvroHttpRequest RecordBuilder. + * @return A new AvroHttpRequest RecordBuilder + */ + public static AvroHttpRequest.Builder newBuilder() { + return new AvroHttpRequest.Builder(); + } + + /** + * Creates a new AvroHttpRequest RecordBuilder by copying an existing Builder. + * @param other The existing builder to copy. + * @return A new AvroHttpRequest RecordBuilder + */ + public static AvroHttpRequest.Builder newBuilder(AvroHttpRequest.Builder other) { + return new AvroHttpRequest.Builder(other); + } + + /** + * Creates a new AvroHttpRequest RecordBuilder by copying an existing AvroHttpRequest instance. + * @param other The existing instance to copy. + * @return A new AvroHttpRequest RecordBuilder + */ + public static AvroHttpRequest.Builder newBuilder(AvroHttpRequest other) { + return new AvroHttpRequest.Builder(other); + } + + /** + * RecordBuilder for AvroHttpRequest instances. + */ + public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase + implements org.apache.avro.data.RecordBuilder { + + private long requestTime; + private ClientIdentifier clientIdentifier; + private ClientIdentifier.Builder clientIdentifierBuilder; + private java.util.List employeeNames; + private Active active; + + /** Creates a new Builder */ + private Builder() { + super(SCHEMA$); + } + + /** + * Creates a Builder by copying an existing Builder. + * @param other The existing Builder to copy. + */ + private Builder(AvroHttpRequest.Builder other) { + super(other); + if (isValidValue(fields()[0], other.requestTime)) { + this.requestTime = data().deepCopy(fields()[0].schema(), other.requestTime); + fieldSetFlags()[0] = true; + } + if (isValidValue(fields()[1], other.clientIdentifier)) { + this.clientIdentifier = data().deepCopy(fields()[1].schema(), other.clientIdentifier); + fieldSetFlags()[1] = true; + } + if (other.hasClientIdentifierBuilder()) { + this.clientIdentifierBuilder = ClientIdentifier.newBuilder(other.getClientIdentifierBuilder()); + } + if (isValidValue(fields()[2], other.employeeNames)) { + this.employeeNames = data().deepCopy(fields()[2].schema(), other.employeeNames); + fieldSetFlags()[2] = true; + } + if (isValidValue(fields()[3], other.active)) { + this.active = data().deepCopy(fields()[3].schema(), other.active); + fieldSetFlags()[3] = true; + } + } + + /** + * Creates a Builder by copying an existing AvroHttpRequest instance + * @param other The existing instance to copy. + */ + private Builder(AvroHttpRequest other) { + super(SCHEMA$); + if (isValidValue(fields()[0], other.requestTime)) { + this.requestTime = data().deepCopy(fields()[0].schema(), other.requestTime); + fieldSetFlags()[0] = true; + } + if (isValidValue(fields()[1], other.clientIdentifier)) { + this.clientIdentifier = data().deepCopy(fields()[1].schema(), other.clientIdentifier); + fieldSetFlags()[1] = true; + } + this.clientIdentifierBuilder = null; + if (isValidValue(fields()[2], other.employeeNames)) { + this.employeeNames = data().deepCopy(fields()[2].schema(), other.employeeNames); + fieldSetFlags()[2] = true; + } + if (isValidValue(fields()[3], other.active)) { + this.active = data().deepCopy(fields()[3].schema(), other.active); + fieldSetFlags()[3] = true; + } + } + + /** + * Gets the value of the 'requestTime' field. + * @return The value. + */ + public java.lang.Long getRequestTime() { + return requestTime; + } + + /** + * Sets the value of the 'requestTime' field. + * @param value The value of 'requestTime'. + * @return This builder. + */ + public AvroHttpRequest.Builder setRequestTime(long value) { + validate(fields()[0], value); + this.requestTime = value; + fieldSetFlags()[0] = true; + return this; + } + + /** + * Checks whether the 'requestTime' field has been set. + * @return True if the 'requestTime' field has been set, false otherwise. + */ + public boolean hasRequestTime() { + return fieldSetFlags()[0]; + } + + + /** + * Clears the value of the 'requestTime' field. + * @return This builder. + */ + public AvroHttpRequest.Builder clearRequestTime() { + fieldSetFlags()[0] = false; + return this; + } + + /** + * Gets the value of the 'clientIdentifier' field. + * @return The value. + */ + public ClientIdentifier getClientIdentifier() { + return clientIdentifier; + } + + /** + * Sets the value of the 'clientIdentifier' field. + * @param value The value of 'clientIdentifier'. + * @return This builder. + */ + public AvroHttpRequest.Builder setClientIdentifier(ClientIdentifier value) { + validate(fields()[1], value); + this.clientIdentifierBuilder = null; + this.clientIdentifier = value; + fieldSetFlags()[1] = true; + return this; + } + + /** + * Checks whether the 'clientIdentifier' field has been set. + * @return True if the 'clientIdentifier' field has been set, false otherwise. + */ + public boolean hasClientIdentifier() { + return fieldSetFlags()[1]; + } + + /** + * Gets the Builder instance for the 'clientIdentifier' field and creates one if it doesn't exist yet. + * @return This builder. + */ + public ClientIdentifier.Builder getClientIdentifierBuilder() { + if (clientIdentifierBuilder == null) { + if (hasClientIdentifier()) { + setClientIdentifierBuilder(ClientIdentifier.newBuilder(clientIdentifier)); + } else { + setClientIdentifierBuilder(ClientIdentifier.newBuilder()); + } + } + return clientIdentifierBuilder; + } + + /** + * Sets the Builder instance for the 'clientIdentifier' field + * @param value The builder instance that must be set. + * @return This builder. + */ + public AvroHttpRequest.Builder setClientIdentifierBuilder(ClientIdentifier.Builder value) { + clearClientIdentifier(); + clientIdentifierBuilder = value; + return this; + } + + /** + * Checks whether the 'clientIdentifier' field has an active Builder instance + * @return True if the 'clientIdentifier' field has an active Builder instance + */ + public boolean hasClientIdentifierBuilder() { + return clientIdentifierBuilder != null; + } + + /** + * Clears the value of the 'clientIdentifier' field. + * @return This builder. + */ + public AvroHttpRequest.Builder clearClientIdentifier() { + clientIdentifier = null; + clientIdentifierBuilder = null; + fieldSetFlags()[1] = false; + return this; + } + + /** + * Gets the value of the 'employeeNames' field. + * @return The value. + */ + public java.util.List getEmployeeNames() { + return employeeNames; + } + + /** + * Sets the value of the 'employeeNames' field. + * @param value The value of 'employeeNames'. + * @return This builder. + */ + public AvroHttpRequest.Builder setEmployeeNames(java.util.List value) { + validate(fields()[2], value); + this.employeeNames = value; + fieldSetFlags()[2] = true; + return this; + } + + /** + * Checks whether the 'employeeNames' field has been set. + * @return True if the 'employeeNames' field has been set, false otherwise. + */ + public boolean hasEmployeeNames() { + return fieldSetFlags()[2]; + } + + + /** + * Clears the value of the 'employeeNames' field. + * @return This builder. + */ + public AvroHttpRequest.Builder clearEmployeeNames() { + employeeNames = null; + fieldSetFlags()[2] = false; + return this; + } + + /** + * Gets the value of the 'active' field. + * @return The value. + */ + public Active getActive() { + return active; + } + + /** + * Sets the value of the 'active' field. + * @param value The value of 'active'. + * @return This builder. + */ + public AvroHttpRequest.Builder setActive(Active value) { + validate(fields()[3], value); + this.active = value; + fieldSetFlags()[3] = true; + return this; + } + + /** + * Checks whether the 'active' field has been set. + * @return True if the 'active' field has been set, false otherwise. + */ + public boolean hasActive() { + return fieldSetFlags()[3]; + } + + + /** + * Clears the value of the 'active' field. + * @return This builder. + */ + public AvroHttpRequest.Builder clearActive() { + active = null; + fieldSetFlags()[3] = false; + return this; + } + + @Override + @SuppressWarnings("unchecked") + public AvroHttpRequest build() { + try { + AvroHttpRequest record = new AvroHttpRequest(); + record.requestTime = fieldSetFlags()[0] ? this.requestTime : (java.lang.Long) defaultValue(fields()[0]); + if (clientIdentifierBuilder != null) { + record.clientIdentifier = this.clientIdentifierBuilder.build(); + } else { + record.clientIdentifier = fieldSetFlags()[1] ? this.clientIdentifier : (ClientIdentifier) defaultValue(fields()[1]); + } + record.employeeNames = fieldSetFlags()[2] ? this.employeeNames : (java.util.List) defaultValue(fields()[2]); + record.active = fieldSetFlags()[3] ? this.active : (Active) defaultValue(fields()[3]); + return record; + } catch (java.lang.Exception e) { + throw new org.apache.avro.AvroRuntimeException(e); + } + } + } + + @SuppressWarnings("unchecked") + private static final org.apache.avro.io.DatumWriter + WRITER$ = (org.apache.avro.io.DatumWriter)MODEL$.createDatumWriter(SCHEMA$); + + @Override public void writeExternal(java.io.ObjectOutput out) + throws java.io.IOException { + WRITER$.write(this, SpecificData.getEncoder(out)); + } + + @SuppressWarnings("unchecked") + private static final org.apache.avro.io.DatumReader + READER$ = (org.apache.avro.io.DatumReader)MODEL$.createDatumReader(SCHEMA$); + + @Override public void readExternal(java.io.ObjectInput in) + throws java.io.IOException { + READER$.read(this, SpecificData.getDecoder(in)); + } + +} diff --git a/apache-avro/src/main/java/com/baeldung/avro/util/model/ClientIdentifier.java b/apache-avro/src/main/java/com/baeldung/avro/util/model/ClientIdentifier.java new file mode 100644 index 0000000000..503dde40df --- /dev/null +++ b/apache-avro/src/main/java/com/baeldung/avro/util/model/ClientIdentifier.java @@ -0,0 +1,308 @@ +/** + * Autogenerated by Avro + * + * DO NOT EDIT DIRECTLY + */ +package com.baeldung.avro.util.model; + +import org.apache.avro.specific.SpecificData; +import org.apache.avro.message.BinaryMessageEncoder; +import org.apache.avro.message.BinaryMessageDecoder; +import org.apache.avro.message.SchemaStore; + +@SuppressWarnings("all") +@org.apache.avro.specific.AvroGenerated +public class ClientIdentifier extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord { + private static final long serialVersionUID = 8754570983127295424L; + public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"ClientIdentifier\",\"namespace\":\"com.baeldung.avro.model\",\"fields\":[{\"name\":\"hostName\",\"type\":\"string\"},{\"name\":\"ipAddress\",\"type\":\"string\"}]}"); + public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; } + + private static SpecificData MODEL$ = new SpecificData(); + + private static final BinaryMessageEncoder ENCODER = + new BinaryMessageEncoder(MODEL$, SCHEMA$); + + private static final BinaryMessageDecoder DECODER = + new BinaryMessageDecoder(MODEL$, SCHEMA$); + + /** + * Return the BinaryMessageDecoder instance used by this class. + */ + public static BinaryMessageDecoder getDecoder() { + return DECODER; + } + + /** + * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}. + * @param resolver a {@link SchemaStore} used to find schemas by fingerprint + */ + public static BinaryMessageDecoder createDecoder(SchemaStore resolver) { + return new BinaryMessageDecoder(MODEL$, SCHEMA$, resolver); + } + + /** Serializes this ClientIdentifier to a ByteBuffer. */ + public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException { + return ENCODER.encode(this); + } + + /** Deserializes a ClientIdentifier from a ByteBuffer. */ + public static ClientIdentifier fromByteBuffer( + java.nio.ByteBuffer b) throws java.io.IOException { + return DECODER.decode(b); + } + + @Deprecated public java.lang.CharSequence hostName; + @Deprecated public java.lang.CharSequence ipAddress; + + /** + * Default constructor. Note that this does not initialize fields + * to their default values from the schema. If that is desired then + * one should use newBuilder(). + */ + public ClientIdentifier() {} + + /** + * All-args constructor. + * @param hostName The new value for hostName + * @param ipAddress The new value for ipAddress + */ + public ClientIdentifier(java.lang.CharSequence hostName, java.lang.CharSequence ipAddress) { + this.hostName = hostName; + this.ipAddress = ipAddress; + } + + public org.apache.avro.Schema getSchema() { return SCHEMA$; } + // Used by DatumWriter. Applications should not call. + public java.lang.Object get(int field$) { + switch (field$) { + case 0: return hostName; + case 1: return ipAddress; + default: throw new org.apache.avro.AvroRuntimeException("Bad index"); + } + } + + // Used by DatumReader. Applications should not call. + @SuppressWarnings(value="unchecked") + public void put(int field$, java.lang.Object value$) { + switch (field$) { + case 0: hostName = (java.lang.CharSequence)value$; break; + case 1: ipAddress = (java.lang.CharSequence)value$; break; + default: throw new org.apache.avro.AvroRuntimeException("Bad index"); + } + } + + /** + * Gets the value of the 'hostName' field. + * @return The value of the 'hostName' field. + */ + public java.lang.CharSequence getHostName() { + return hostName; + } + + /** + * Sets the value of the 'hostName' field. + * @param value the value to set. + */ + public void setHostName(java.lang.CharSequence value) { + this.hostName = value; + } + + /** + * Gets the value of the 'ipAddress' field. + * @return The value of the 'ipAddress' field. + */ + public java.lang.CharSequence getIpAddress() { + return ipAddress; + } + + /** + * Sets the value of the 'ipAddress' field. + * @param value the value to set. + */ + public void setIpAddress(java.lang.CharSequence value) { + this.ipAddress = value; + } + + /** + * Creates a new ClientIdentifier RecordBuilder. + * @return A new ClientIdentifier RecordBuilder + */ + public static ClientIdentifier.Builder newBuilder() { + return new ClientIdentifier.Builder(); + } + + /** + * Creates a new ClientIdentifier RecordBuilder by copying an existing Builder. + * @param other The existing builder to copy. + * @return A new ClientIdentifier RecordBuilder + */ + public static ClientIdentifier.Builder newBuilder(ClientIdentifier.Builder other) { + return new ClientIdentifier.Builder(other); + } + + /** + * Creates a new ClientIdentifier RecordBuilder by copying an existing ClientIdentifier instance. + * @param other The existing instance to copy. + * @return A new ClientIdentifier RecordBuilder + */ + public static ClientIdentifier.Builder newBuilder(ClientIdentifier other) { + return new ClientIdentifier.Builder(other); + } + + /** + * RecordBuilder for ClientIdentifier instances. + */ + public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase + implements org.apache.avro.data.RecordBuilder { + + private java.lang.CharSequence hostName; + private java.lang.CharSequence ipAddress; + + /** Creates a new Builder */ + private Builder() { + super(SCHEMA$); + } + + /** + * Creates a Builder by copying an existing Builder. + * @param other The existing Builder to copy. + */ + private Builder(ClientIdentifier.Builder other) { + super(other); + if (isValidValue(fields()[0], other.hostName)) { + this.hostName = data().deepCopy(fields()[0].schema(), other.hostName); + fieldSetFlags()[0] = true; + } + if (isValidValue(fields()[1], other.ipAddress)) { + this.ipAddress = data().deepCopy(fields()[1].schema(), other.ipAddress); + fieldSetFlags()[1] = true; + } + } + + /** + * Creates a Builder by copying an existing ClientIdentifier instance + * @param other The existing instance to copy. + */ + private Builder(ClientIdentifier other) { + super(SCHEMA$); + if (isValidValue(fields()[0], other.hostName)) { + this.hostName = data().deepCopy(fields()[0].schema(), other.hostName); + fieldSetFlags()[0] = true; + } + if (isValidValue(fields()[1], other.ipAddress)) { + this.ipAddress = data().deepCopy(fields()[1].schema(), other.ipAddress); + fieldSetFlags()[1] = true; + } + } + + /** + * Gets the value of the 'hostName' field. + * @return The value. + */ + public java.lang.CharSequence getHostName() { + return hostName; + } + + /** + * Sets the value of the 'hostName' field. + * @param value The value of 'hostName'. + * @return This builder. + */ + public ClientIdentifier.Builder setHostName(java.lang.CharSequence value) { + validate(fields()[0], value); + this.hostName = value; + fieldSetFlags()[0] = true; + return this; + } + + /** + * Checks whether the 'hostName' field has been set. + * @return True if the 'hostName' field has been set, false otherwise. + */ + public boolean hasHostName() { + return fieldSetFlags()[0]; + } + + + /** + * Clears the value of the 'hostName' field. + * @return This builder. + */ + public ClientIdentifier.Builder clearHostName() { + hostName = null; + fieldSetFlags()[0] = false; + return this; + } + + /** + * Gets the value of the 'ipAddress' field. + * @return The value. + */ + public java.lang.CharSequence getIpAddress() { + return ipAddress; + } + + /** + * Sets the value of the 'ipAddress' field. + * @param value The value of 'ipAddress'. + * @return This builder. + */ + public ClientIdentifier.Builder setIpAddress(java.lang.CharSequence value) { + validate(fields()[1], value); + this.ipAddress = value; + fieldSetFlags()[1] = true; + return this; + } + + /** + * Checks whether the 'ipAddress' field has been set. + * @return True if the 'ipAddress' field has been set, false otherwise. + */ + public boolean hasIpAddress() { + return fieldSetFlags()[1]; + } + + + /** + * Clears the value of the 'ipAddress' field. + * @return This builder. + */ + public ClientIdentifier.Builder clearIpAddress() { + ipAddress = null; + fieldSetFlags()[1] = false; + return this; + } + + @Override + @SuppressWarnings("unchecked") + public ClientIdentifier build() { + try { + ClientIdentifier record = new ClientIdentifier(); + record.hostName = fieldSetFlags()[0] ? this.hostName : (java.lang.CharSequence) defaultValue(fields()[0]); + record.ipAddress = fieldSetFlags()[1] ? this.ipAddress : (java.lang.CharSequence) defaultValue(fields()[1]); + return record; + } catch (java.lang.Exception e) { + throw new org.apache.avro.AvroRuntimeException(e); + } + } + } + + @SuppressWarnings("unchecked") + private static final org.apache.avro.io.DatumWriter + WRITER$ = (org.apache.avro.io.DatumWriter)MODEL$.createDatumWriter(SCHEMA$); + + @Override public void writeExternal(java.io.ObjectOutput out) + throws java.io.IOException { + WRITER$.write(this, SpecificData.getEncoder(out)); + } + + @SuppressWarnings("unchecked") + private static final org.apache.avro.io.DatumReader + READER$ = (org.apache.avro.io.DatumReader)MODEL$.createDatumReader(SCHEMA$); + + @Override public void readExternal(java.io.ObjectInput in) + throws java.io.IOException { + READER$.read(this, SpecificData.getDecoder(in)); + } + +} diff --git a/apache-avro/src/main/java/com/baeldung/avro/util/serealization/AvroDeSerealizer.java b/apache-avro/src/main/java/com/baeldung/avro/util/serealization/AvroDeSerealizer.java new file mode 100644 index 0000000000..d2219a45f2 --- /dev/null +++ b/apache-avro/src/main/java/com/baeldung/avro/util/serealization/AvroDeSerealizer.java @@ -0,0 +1,33 @@ +package com.baeldung.avro.util.serealization; + +import com.baeldung.avro.util.model.AvroHttpRequest; +import org.apache.avro.io.DatumReader; +import org.apache.avro.io.Decoder; +import org.apache.avro.io.DecoderFactory; +import org.apache.avro.specific.SpecificDatumReader; + +import java.io.IOException; + +public class AvroDeSerealizer { + +public AvroHttpRequest deSerealizeAvroHttpRequestJSON(byte[] data){ + DatumReader reader = new SpecificDatumReader<>(AvroHttpRequest.class); + Decoder decoder = null; + try { + decoder = DecoderFactory.get().jsonDecoder(AvroHttpRequest.getClassSchema(), new String(data)); + return reader.read(null, decoder); + } catch (IOException e) { + return null; + } +} + +public AvroHttpRequest deSerealizeAvroHttpRequestBinary(byte[] data){ + DatumReader employeeReader = new SpecificDatumReader<>(AvroHttpRequest.class); + Decoder decoder = DecoderFactory.get().binaryDecoder(data, null); + try { + return employeeReader.read(null, decoder); + } catch (IOException e) { + return null; + } +} +} diff --git a/apache-avro/src/main/java/com/baeldung/avro/util/serealization/AvroSerealizer.java b/apache-avro/src/main/java/com/baeldung/avro/util/serealization/AvroSerealizer.java new file mode 100644 index 0000000000..f56c89e201 --- /dev/null +++ b/apache-avro/src/main/java/com/baeldung/avro/util/serealization/AvroSerealizer.java @@ -0,0 +1,44 @@ +package com.baeldung.avro.util.serealization; + +import com.baeldung.avro.util.model.AvroHttpRequest; +import org.apache.avro.io.*; +import org.apache.avro.specific.SpecificDatumWriter; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +public class AvroSerealizer { + +public byte[] serealizeAvroHttpRequestJSON(AvroHttpRequest request){ + DatumWriter writer = new SpecificDatumWriter<>(AvroHttpRequest.class); + byte[] data = new byte[0]; + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Encoder jsonEncoder = null; + try { + jsonEncoder = EncoderFactory.get().jsonEncoder(AvroHttpRequest.getClassSchema(), stream); + writer.write(request, jsonEncoder); + jsonEncoder.flush(); + data = stream.toByteArray(); + } catch (IOException e) { + data =null; + } + return data; +} + +public byte[] serealizeAvroHttpRequestBinary(AvroHttpRequest request){ + DatumWriter writer = new SpecificDatumWriter<>(AvroHttpRequest.class); + byte[] data = new byte[0]; + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Encoder jsonEncoder = EncoderFactory.get().binaryEncoder(stream,null); + try { + writer.write(request, jsonEncoder); + jsonEncoder.flush(); + data = stream.toByteArray(); + } catch (IOException e) { + data = null; + } + + return data; +} + +} diff --git a/apache-avro/src/main/resources/avroHttpRequest-schema.avsc b/apache-avro/src/main/resources/avroHttpRequest-schema.avsc new file mode 100644 index 0000000000..18179a9cde --- /dev/null +++ b/apache-avro/src/main/resources/avroHttpRequest-schema.avsc @@ -0,0 +1,47 @@ +{ + "type":"record", + "name":"AvroHttpRequest", + "namespace":"com.baeldung.avro.model", + "fields":[ + { + "name":"requestTime", + "type":"long" + }, + { + "name":"clientIdentifier", + "type":{ + "type":"record", + "name":"ClientIdentifier", + "fields":[ + { + "name":"hostName", + "type":"string" + }, + { + "name":"ipAddress", + "type":"string" + } + ] + } + }, + { + "name":"employeeNames", + "type":{ + "type":"array", + "items":"string" + }, + "default":null + }, + { + "name":"active", + "type":{ + "type":"enum", + "name":"Active", + "symbols":[ + "YES", + "NO" + ] + } + } + ] +} \ No newline at end of file diff --git a/apache-avro/src/test/java/com/baeldung/avro/util/serealization/AvroSerealizerDeSerealizerTest.java b/apache-avro/src/test/java/com/baeldung/avro/util/serealization/AvroSerealizerDeSerealizerTest.java new file mode 100644 index 0000000000..937a4ae650 --- /dev/null +++ b/apache-avro/src/test/java/com/baeldung/avro/util/serealization/AvroSerealizerDeSerealizerTest.java @@ -0,0 +1,75 @@ +package com.baeldung.avro.util.serealization; + +import com.baeldung.avro.util.model.Active; +import com.baeldung.avro.util.model.AvroHttpRequest; +import com.baeldung.avro.util.model.ClientIdentifier; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static org.junit.Assert.*; + +public class AvroSerealizerDeSerealizerTest { + + AvroSerealizer serealizer; + AvroDeSerealizer deSerealizer; + AvroHttpRequest request; + + @Before + public void setUp() throws Exception { + serealizer = new AvroSerealizer(); + deSerealizer = new AvroDeSerealizer(); + + ClientIdentifier clientIdentifier = ClientIdentifier.newBuilder(). + setHostName("localhost").setIpAddress("255.255.255.0").build(); + + List employees = new ArrayList(); + employees.add("James"); + employees.add("Alice"); + employees.add("David"); + employees.add("Han"); + + request = AvroHttpRequest.newBuilder().setRequestTime(01l) + .setActive(Active.YES).setClientIdentifier(clientIdentifier) + .setEmployeeNames(employees).build(); + } + + @After + public void tearDown() throws Exception { + } + +@Test +public void WhenSerialized_UsingJSONEncoder_ObjectGetsSerialized(){ + byte[] data = serealizer.serealizeAvroHttpRequestJSON(request); + assertTrue(Objects.nonNull(data)); + assertTrue(data.length > 0); +} + +@Test +public void WhenSerialized_UsingBinaryEncoder_ObjectGetsSerialized(){ + byte[] data = serealizer.serealizeAvroHttpRequestBinary(request); + assertTrue(Objects.nonNull(data)); + assertTrue(data.length > 0); +} + +@Test +public void WhenDeserialize_UsingJSONDecoder_ActualAndExpectedObjectsAreEqual(){ + byte[] data = serealizer.serealizeAvroHttpRequestJSON(request); + AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestJSON(data); + assertEquals(actualRequest,request); + assertTrue(actualRequest.getRequestTime().equals(request.getRequestTime())); +} + +@Test +public void WhenDeserialize_UsingBinaryecoder_ActualAndExpectedObjectsAreEqual(){ + byte[] data = serealizer.serealizeAvroHttpRequestBinary(request); + AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestBinary(data); + assertEquals(actualRequest,request); + assertTrue(actualRequest.getRequestTime().equals(request.getRequestTime())); +} +} +