diff --git a/apache-libraries/parent.avro b/apache-libraries/parent.avro index 59f8e40935df..6f78bd68773a 100644 Binary files a/apache-libraries/parent.avro and b/apache-libraries/parent.avro differ diff --git a/apache-libraries/pom.xml b/apache-libraries/pom.xml index c7a942df57ec..c1b27f96da3a 100644 --- a/apache-libraries/pom.xml +++ b/apache-libraries/pom.xml @@ -23,12 +23,12 @@ org.apache.avro avro-compiler - ${avro.version} + ${apache.avro.version} org.apache.avro avro-maven-plugin - ${avro.version} + ${apache.avro.version} @@ -88,8 +88,8 @@ avro-maven-plugin ${apache.avro.version} - src/main/java/com/baeldung/apache/avro/schemas - src/main/java + ${project.basedir}/src/main/resources/schemas + ${project.build.directory}/generated-sources/avro/ String @@ -101,11 +101,18 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 15 + 15 + + - 1.8.2 3.0.1 3.1.01.8.4 6.4.0 diff --git a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Active.java b/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Active.java deleted file mode 100644 index 6f76551b7a69..000000000000 --- a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Active.java +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Autogenerated by Avro - * - * DO NOT EDIT DIRECTLY - */ -package com.baeldung.apache.avro.generated; -@org.apache.avro.specific.AvroGenerated -public enum Active implements org.apache.avro.generic.GenericEnumSymbol { - YES, NO ; - public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"enum\",\"name\":\"Active\",\"namespace\":\"com.baeldung.apache.avro.generated\",\"symbols\":[\"YES\",\"NO\"]}"); - public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; } - - @Override - public org.apache.avro.Schema getSchema() { return SCHEMA$; } -} diff --git a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/AvroHttpRequest.java b/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/AvroHttpRequest.java deleted file mode 100644 index c5e4d3263182..000000000000 --- a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/AvroHttpRequest.java +++ /dev/null @@ -1,649 +0,0 @@ -/** - * Autogenerated by Avro - * - * DO NOT EDIT DIRECTLY - */ -package com.baeldung.apache.avro.generated; - -import org.apache.avro.specific.SpecificData; -import org.apache.avro.message.BinaryMessageEncoder; -import org.apache.avro.message.BinaryMessageDecoder; -import org.apache.avro.message.SchemaStore; - -@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 = -4208236535214244520L; - - - public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"AvroHttpRequest\",\"namespace\":\"com.baeldung.apache.avro.generated\",\"fields\":[{\"name\":\"requestTime\",\"type\":\"long\"},{\"name\":\"clientIdentifier\",\"type\":{\"type\":\"record\",\"name\":\"ClientIdentifier\",\"fields\":[{\"name\":\"hostName\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"ipAddress\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}]}},{\"name\":\"employeeNames\",\"type\":{\"type\":\"array\",\"items\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},\"default\":[\"John Doe\",\"Jane Smith\"]},{\"name\":\"active\",\"type\":{\"type\":\"enum\",\"name\":\"Active\",\"symbols\":[\"YES\",\"NO\"]}}]}"); - public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; } - - private static final SpecificData MODEL$ = new SpecificData(); - - private static final BinaryMessageEncoder ENCODER = - new BinaryMessageEncoder<>(MODEL$, SCHEMA$); - - private static final BinaryMessageDecoder DECODER = - new BinaryMessageDecoder<>(MODEL$, SCHEMA$); - - /** - * Return the BinaryMessageEncoder instance used by this class. - * @return the message encoder used by this class - */ - public static BinaryMessageEncoder getEncoder() { - return ENCODER; - } - - /** - * Return the BinaryMessageDecoder instance used by this class. - * @return the message decoder 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 - * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore - */ - public static BinaryMessageDecoder createDecoder(SchemaStore resolver) { - return new BinaryMessageDecoder<>(MODEL$, SCHEMA$, resolver); - } - - /** - * Serializes this AvroHttpRequest to a ByteBuffer. - * @return a buffer holding the serialized data for this instance - * @throws java.io.IOException if this instance could not be serialized - */ - public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException { - return ENCODER.encode(this); - } - - /** - * Deserializes a AvroHttpRequest from a ByteBuffer. - * @param b a byte buffer holding serialized data for an instance of this class - * @return a AvroHttpRequest instance decoded from the given buffer - * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class - */ - public static AvroHttpRequest fromByteBuffer( - java.nio.ByteBuffer b) throws java.io.IOException { - return DECODER.decode(b); - } - - private long requestTime; - private com.baeldung.apache.avro.generated.ClientIdentifier clientIdentifier; - private java.util.List employeeNames; - private com.baeldung.apache.avro.generated.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, com.baeldung.apache.avro.generated.ClientIdentifier clientIdentifier, java.util.List employeeNames, com.baeldung.apache.avro.generated.Active active) { - this.requestTime = requestTime; - this.clientIdentifier = clientIdentifier; - this.employeeNames = employeeNames; - this.active = active; - } - - @Override - public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; } - - @Override - public org.apache.avro.Schema getSchema() { return SCHEMA$; } - - // Used by DatumWriter. Applications should not call. - @Override - 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 IndexOutOfBoundsException("Invalid index: " + field$); - } - } - - // Used by DatumReader. Applications should not call. - @Override - @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 = (com.baeldung.apache.avro.generated.ClientIdentifier)value$; break; - case 2: employeeNames = (java.util.List)value$; break; - case 3: active = (com.baeldung.apache.avro.generated.Active)value$; break; - default: throw new IndexOutOfBoundsException("Invalid index: " + field$); - } - } - - /** - * Gets the value of the 'requestTime' field. - * @return The value of the 'requestTime' field. - */ - public long getRequestTime() { - return requestTime; - } - - - /** - * Sets the value of the 'requestTime' field. - * @param value the value to set. - */ - public void setRequestTime(long value) { - this.requestTime = value; - } - - /** - * Gets the value of the 'clientIdentifier' field. - * @return The value of the 'clientIdentifier' field. - */ - public com.baeldung.apache.avro.generated.ClientIdentifier getClientIdentifier() { - return clientIdentifier; - } - - - /** - * Sets the value of the 'clientIdentifier' field. - * @param value the value to set. - */ - public void setClientIdentifier(com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.Active getActive() { - return active; - } - - - /** - * Sets the value of the 'active' field. - * @param value the value to set. - */ - public void setActive(com.baeldung.apache.avro.generated.Active value) { - this.active = value; - } - - /** - * Creates a new AvroHttpRequest RecordBuilder. - * @return A new AvroHttpRequest RecordBuilder - */ - public static com.baeldung.apache.avro.generated.AvroHttpRequest.Builder newBuilder() { - return new com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.AvroHttpRequest.Builder newBuilder(com.baeldung.apache.avro.generated.AvroHttpRequest.Builder other) { - if (other == null) { - return new com.baeldung.apache.avro.generated.AvroHttpRequest.Builder(); - } else { - return new com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.AvroHttpRequest.Builder newBuilder(com.baeldung.apache.avro.generated.AvroHttpRequest other) { - if (other == null) { - return new com.baeldung.apache.avro.generated.AvroHttpRequest.Builder(); - } else { - return new com.baeldung.apache.avro.generated.AvroHttpRequest.Builder(other); - } - } - - /** - * RecordBuilder for AvroHttpRequest instances. - */ - @org.apache.avro.specific.AvroGenerated - public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase - implements org.apache.avro.data.RecordBuilder { - - private long requestTime; - private com.baeldung.apache.avro.generated.ClientIdentifier clientIdentifier; - private com.baeldung.apache.avro.generated.ClientIdentifier.Builder clientIdentifierBuilder; - private java.util.List employeeNames; - private com.baeldung.apache.avro.generated.Active active; - - /** Creates a new Builder */ - private Builder() { - super(SCHEMA$, MODEL$); - } - - /** - * Creates a Builder by copying an existing Builder. - * @param other The existing Builder to copy. - */ - private Builder(com.baeldung.apache.avro.generated.AvroHttpRequest.Builder other) { - super(other); - if (isValidValue(fields()[0], other.requestTime)) { - this.requestTime = data().deepCopy(fields()[0].schema(), other.requestTime); - fieldSetFlags()[0] = other.fieldSetFlags()[0]; - } - if (isValidValue(fields()[1], other.clientIdentifier)) { - this.clientIdentifier = data().deepCopy(fields()[1].schema(), other.clientIdentifier); - fieldSetFlags()[1] = other.fieldSetFlags()[1]; - } - if (other.hasClientIdentifierBuilder()) { - this.clientIdentifierBuilder = com.baeldung.apache.avro.generated.ClientIdentifier.newBuilder(other.getClientIdentifierBuilder()); - } - if (isValidValue(fields()[2], other.employeeNames)) { - this.employeeNames = data().deepCopy(fields()[2].schema(), other.employeeNames); - fieldSetFlags()[2] = other.fieldSetFlags()[2]; - } - if (isValidValue(fields()[3], other.active)) { - this.active = data().deepCopy(fields()[3].schema(), other.active); - fieldSetFlags()[3] = other.fieldSetFlags()[3]; - } - } - - /** - * Creates a Builder by copying an existing AvroHttpRequest instance - * @param other The existing instance to copy. - */ - private Builder(com.baeldung.apache.avro.generated.AvroHttpRequest other) { - super(SCHEMA$, MODEL$); - 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 long getRequestTime() { - return requestTime; - } - - - /** - * Sets the value of the 'requestTime' field. - * @param value The value of 'requestTime'. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.AvroHttpRequest.Builder clearRequestTime() { - fieldSetFlags()[0] = false; - return this; - } - - /** - * Gets the value of the 'clientIdentifier' field. - * @return The value. - */ - public com.baeldung.apache.avro.generated.ClientIdentifier getClientIdentifier() { - return clientIdentifier; - } - - - /** - * Sets the value of the 'clientIdentifier' field. - * @param value The value of 'clientIdentifier'. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.AvroHttpRequest.Builder setClientIdentifier(com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.ClientIdentifier.Builder getClientIdentifierBuilder() { - if (clientIdentifierBuilder == null) { - if (hasClientIdentifier()) { - setClientIdentifierBuilder(com.baeldung.apache.avro.generated.ClientIdentifier.newBuilder(clientIdentifier)); - } else { - setClientIdentifierBuilder(com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.AvroHttpRequest.Builder setClientIdentifierBuilder(com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.AvroHttpRequest.Builder clearEmployeeNames() { - employeeNames = null; - fieldSetFlags()[2] = false; - return this; - } - - /** - * Gets the value of the 'active' field. - * @return The value. - */ - public com.baeldung.apache.avro.generated.Active getActive() { - return active; - } - - - /** - * Sets the value of the 'active' field. - * @param value The value of 'active'. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.AvroHttpRequest.Builder setActive(com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.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) { - try { - record.clientIdentifier = this.clientIdentifierBuilder.build(); - } catch (org.apache.avro.AvroMissingFieldException e) { - e.addParentField(record.getSchema().getField("clientIdentifier")); - throw e; - } - } else { - record.clientIdentifier = fieldSetFlags()[1] ? this.clientIdentifier : (com.baeldung.apache.avro.generated.ClientIdentifier) defaultValue(fields()[1]); - } - record.employeeNames = fieldSetFlags()[2] ? this.employeeNames : (java.util.List) defaultValue(fields()[2]); - record.active = fieldSetFlags()[3] ? this.active : (com.baeldung.apache.avro.generated.Active) defaultValue(fields()[3]); - return record; - } catch (org.apache.avro.AvroMissingFieldException e) { - throw e; - } 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)); - } - - @Override protected boolean hasCustomCoders() { return true; } - - @Override public void customEncode(org.apache.avro.io.Encoder out) - throws java.io.IOException - { - out.writeLong(this.requestTime); - - this.clientIdentifier.customEncode(out); - - long size0 = this.employeeNames.size(); - out.writeArrayStart(); - out.setItemCount(size0); - long actualSize0 = 0; - for (java.lang.String e0: this.employeeNames) { - actualSize0++; - out.startItem(); - out.writeString(e0); - } - out.writeArrayEnd(); - if (actualSize0 != size0) - throw new java.util.ConcurrentModificationException("Array-size written was " + size0 + ", but element count was " + actualSize0 + "."); - - out.writeEnum(this.active.ordinal()); - - } - - @Override public void customDecode(org.apache.avro.io.ResolvingDecoder in) - throws java.io.IOException - { - org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff(); - if (fieldOrder == null) { - this.requestTime = in.readLong(); - - if (this.clientIdentifier == null) { - this.clientIdentifier = new com.baeldung.apache.avro.generated.ClientIdentifier(); - } - this.clientIdentifier.customDecode(in); - - long size0 = in.readArrayStart(); - java.util.List a0 = this.employeeNames; - if (a0 == null) { - a0 = new SpecificData.Array((int)size0, SCHEMA$.getField("employeeNames").schema()); - this.employeeNames = a0; - } else a0.clear(); - SpecificData.Array ga0 = (a0 instanceof SpecificData.Array ? (SpecificData.Array)a0 : null); - for ( ; 0 < size0; size0 = in.arrayNext()) { - for ( ; size0 != 0; size0--) { - java.lang.String e0 = (ga0 != null ? ga0.peek() : null); - e0 = in.readString(); - a0.add(e0); - } - } - - this.active = com.baeldung.apache.avro.generated.Active.values()[in.readEnum()]; - - } else { - for (int i = 0; i < 4; i++) { - switch (fieldOrder[i].pos()) { - case 0: - this.requestTime = in.readLong(); - break; - - case 1: - if (this.clientIdentifier == null) { - this.clientIdentifier = new com.baeldung.apache.avro.generated.ClientIdentifier(); - } - this.clientIdentifier.customDecode(in); - break; - - case 2: - long size0 = in.readArrayStart(); - java.util.List a0 = this.employeeNames; - if (a0 == null) { - a0 = new SpecificData.Array((int)size0, SCHEMA$.getField("employeeNames").schema()); - this.employeeNames = a0; - } else a0.clear(); - SpecificData.Array ga0 = (a0 instanceof SpecificData.Array ? (SpecificData.Array)a0 : null); - for ( ; 0 < size0; size0 = in.arrayNext()) { - for ( ; size0 != 0; size0--) { - java.lang.String e0 = (ga0 != null ? ga0.peek() : null); - e0 = in.readString(); - a0.add(e0); - } - } - break; - - case 3: - this.active = com.baeldung.apache.avro.generated.Active.values()[in.readEnum()]; - break; - - default: - throw new java.io.IOException("Corrupt ResolvingDecoder."); - } - } - } - } -} - - - - - - - - - - diff --git a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Child.java b/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Child.java deleted file mode 100644 index 2261a4bfdef4..000000000000 --- a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Child.java +++ /dev/null @@ -1,319 +0,0 @@ -/** - * Autogenerated by Avro - * - * DO NOT EDIT DIRECTLY - */ -package com.baeldung.apache.avro.generated; - -import org.apache.avro.generic.GenericArray; -import org.apache.avro.specific.SpecificData; -import org.apache.avro.util.Utf8; -import org.apache.avro.message.BinaryMessageEncoder; -import org.apache.avro.message.BinaryMessageDecoder; -import org.apache.avro.message.SchemaStore; - -@org.apache.avro.specific.AvroGenerated -public class Child extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord { - private static final long serialVersionUID = -1955260180671550364L; - - - public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"Child\",\"namespace\":\"com.baeldung.apache.avro.generated\",\"fields\":[{\"name\":\"name\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}]}"); - public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; } - - private static final SpecificData MODEL$ = new SpecificData(); - - private static final BinaryMessageEncoder ENCODER = - new BinaryMessageEncoder<>(MODEL$, SCHEMA$); - - private static final BinaryMessageDecoder DECODER = - new BinaryMessageDecoder<>(MODEL$, SCHEMA$); - - /** - * Return the BinaryMessageEncoder instance used by this class. - * @return the message encoder used by this class - */ - public static BinaryMessageEncoder getEncoder() { - return ENCODER; - } - - /** - * Return the BinaryMessageDecoder instance used by this class. - * @return the message decoder 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 - * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore - */ - public static BinaryMessageDecoder createDecoder(SchemaStore resolver) { - return new BinaryMessageDecoder<>(MODEL$, SCHEMA$, resolver); - } - - /** - * Serializes this Child to a ByteBuffer. - * @return a buffer holding the serialized data for this instance - * @throws java.io.IOException if this instance could not be serialized - */ - public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException { - return ENCODER.encode(this); - } - - /** - * Deserializes a Child from a ByteBuffer. - * @param b a byte buffer holding serialized data for an instance of this class - * @return a Child instance decoded from the given buffer - * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class - */ - public static Child fromByteBuffer( - java.nio.ByteBuffer b) throws java.io.IOException { - return DECODER.decode(b); - } - - private java.lang.String name; - - /** - * 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 Child() {} - - /** - * All-args constructor. - * @param name The new value for name - */ - public Child(java.lang.String name) { - this.name = name; - } - - @Override - public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; } - - @Override - public org.apache.avro.Schema getSchema() { return SCHEMA$; } - - // Used by DatumWriter. Applications should not call. - @Override - public java.lang.Object get(int field$) { - switch (field$) { - case 0: return name; - default: throw new IndexOutOfBoundsException("Invalid index: " + field$); - } - } - - // Used by DatumReader. Applications should not call. - @Override - @SuppressWarnings(value="unchecked") - public void put(int field$, java.lang.Object value$) { - switch (field$) { - case 0: name = value$ != null ? value$.toString() : null; break; - default: throw new IndexOutOfBoundsException("Invalid index: " + field$); - } - } - - /** - * Gets the value of the 'name' field. - * @return The value of the 'name' field. - */ - public java.lang.String getName() { - return name; - } - - - /** - * Sets the value of the 'name' field. - * @param value the value to set. - */ - public void setName(java.lang.String value) { - this.name = value; - } - - /** - * Creates a new Child RecordBuilder. - * @return A new Child RecordBuilder - */ - public static com.baeldung.apache.avro.generated.Child.Builder newBuilder() { - return new com.baeldung.apache.avro.generated.Child.Builder(); - } - - /** - * Creates a new Child RecordBuilder by copying an existing Builder. - * @param other The existing builder to copy. - * @return A new Child RecordBuilder - */ - public static com.baeldung.apache.avro.generated.Child.Builder newBuilder(com.baeldung.apache.avro.generated.Child.Builder other) { - if (other == null) { - return new com.baeldung.apache.avro.generated.Child.Builder(); - } else { - return new com.baeldung.apache.avro.generated.Child.Builder(other); - } - } - - /** - * Creates a new Child RecordBuilder by copying an existing Child instance. - * @param other The existing instance to copy. - * @return A new Child RecordBuilder - */ - public static com.baeldung.apache.avro.generated.Child.Builder newBuilder(com.baeldung.apache.avro.generated.Child other) { - if (other == null) { - return new com.baeldung.apache.avro.generated.Child.Builder(); - } else { - return new com.baeldung.apache.avro.generated.Child.Builder(other); - } - } - - /** - * RecordBuilder for Child instances. - */ - @org.apache.avro.specific.AvroGenerated - public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase - implements org.apache.avro.data.RecordBuilder { - - private java.lang.String name; - - /** Creates a new Builder */ - private Builder() { - super(SCHEMA$, MODEL$); - } - - /** - * Creates a Builder by copying an existing Builder. - * @param other The existing Builder to copy. - */ - private Builder(com.baeldung.apache.avro.generated.Child.Builder other) { - super(other); - if (isValidValue(fields()[0], other.name)) { - this.name = data().deepCopy(fields()[0].schema(), other.name); - fieldSetFlags()[0] = other.fieldSetFlags()[0]; - } - } - - /** - * Creates a Builder by copying an existing Child instance - * @param other The existing instance to copy. - */ - private Builder(com.baeldung.apache.avro.generated.Child other) { - super(SCHEMA$, MODEL$); - if (isValidValue(fields()[0], other.name)) { - this.name = data().deepCopy(fields()[0].schema(), other.name); - fieldSetFlags()[0] = true; - } - } - - /** - * Gets the value of the 'name' field. - * @return The value. - */ - public java.lang.String getName() { - return name; - } - - - /** - * Sets the value of the 'name' field. - * @param value The value of 'name'. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.Child.Builder setName(java.lang.String value) { - validate(fields()[0], value); - this.name = value; - fieldSetFlags()[0] = true; - return this; - } - - /** - * Checks whether the 'name' field has been set. - * @return True if the 'name' field has been set, false otherwise. - */ - public boolean hasName() { - return fieldSetFlags()[0]; - } - - - /** - * Clears the value of the 'name' field. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.Child.Builder clearName() { - name = null; - fieldSetFlags()[0] = false; - return this; - } - - @Override - @SuppressWarnings("unchecked") - public Child build() { - try { - Child record = new Child(); - record.name = fieldSetFlags()[0] ? this.name : (java.lang.String) defaultValue(fields()[0]); - return record; - } catch (org.apache.avro.AvroMissingFieldException e) { - throw e; - } 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)); - } - - @Override protected boolean hasCustomCoders() { return true; } - - @Override public void customEncode(org.apache.avro.io.Encoder out) - throws java.io.IOException - { - out.writeString(this.name); - - } - - @Override public void customDecode(org.apache.avro.io.ResolvingDecoder in) - throws java.io.IOException - { - org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff(); - if (fieldOrder == null) { - this.name = in.readString(); - - } else { - for (int i = 0; i < 1; i++) { - switch (fieldOrder[i].pos()) { - case 0: - this.name = in.readString(); - break; - - default: - throw new java.io.IOException("Corrupt ResolvingDecoder."); - } - } - } - } -} - - - - - - - - - - diff --git a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/ClientIdentifier.java b/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/ClientIdentifier.java deleted file mode 100644 index fd4d27e3c605..000000000000 --- a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/ClientIdentifier.java +++ /dev/null @@ -1,399 +0,0 @@ -/** - * Autogenerated by Avro - * - * DO NOT EDIT DIRECTLY - */ -package com.baeldung.apache.avro.generated; - -import org.apache.avro.generic.GenericArray; -import org.apache.avro.specific.SpecificData; -import org.apache.avro.util.Utf8; -import org.apache.avro.message.BinaryMessageEncoder; -import org.apache.avro.message.BinaryMessageDecoder; -import org.apache.avro.message.SchemaStore; - -@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 = -1549393766288162557L; - - - public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"ClientIdentifier\",\"namespace\":\"com.baeldung.apache.avro.generated\",\"fields\":[{\"name\":\"hostName\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"ipAddress\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}]}"); - public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; } - - private static final SpecificData MODEL$ = new SpecificData(); - - private static final BinaryMessageEncoder ENCODER = - new BinaryMessageEncoder<>(MODEL$, SCHEMA$); - - private static final BinaryMessageDecoder DECODER = - new BinaryMessageDecoder<>(MODEL$, SCHEMA$); - - /** - * Return the BinaryMessageEncoder instance used by this class. - * @return the message encoder used by this class - */ - public static BinaryMessageEncoder getEncoder() { - return ENCODER; - } - - /** - * Return the BinaryMessageDecoder instance used by this class. - * @return the message decoder 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 - * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore - */ - public static BinaryMessageDecoder createDecoder(SchemaStore resolver) { - return new BinaryMessageDecoder<>(MODEL$, SCHEMA$, resolver); - } - - /** - * Serializes this ClientIdentifier to a ByteBuffer. - * @return a buffer holding the serialized data for this instance - * @throws java.io.IOException if this instance could not be serialized - */ - public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException { - return ENCODER.encode(this); - } - - /** - * Deserializes a ClientIdentifier from a ByteBuffer. - * @param b a byte buffer holding serialized data for an instance of this class - * @return a ClientIdentifier instance decoded from the given buffer - * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class - */ - public static ClientIdentifier fromByteBuffer( - java.nio.ByteBuffer b) throws java.io.IOException { - return DECODER.decode(b); - } - - private java.lang.String hostName; - private java.lang.String 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.String hostName, java.lang.String ipAddress) { - this.hostName = hostName; - this.ipAddress = ipAddress; - } - - @Override - public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; } - - @Override - public org.apache.avro.Schema getSchema() { return SCHEMA$; } - - // Used by DatumWriter. Applications should not call. - @Override - public java.lang.Object get(int field$) { - switch (field$) { - case 0: return hostName; - case 1: return ipAddress; - default: throw new IndexOutOfBoundsException("Invalid index: " + field$); - } - } - - // Used by DatumReader. Applications should not call. - @Override - @SuppressWarnings(value="unchecked") - public void put(int field$, java.lang.Object value$) { - switch (field$) { - case 0: hostName = value$ != null ? value$.toString() : null; break; - case 1: ipAddress = value$ != null ? value$.toString() : null; break; - default: throw new IndexOutOfBoundsException("Invalid index: " + field$); - } - } - - /** - * Gets the value of the 'hostName' field. - * @return The value of the 'hostName' field. - */ - public java.lang.String getHostName() { - return hostName; - } - - - /** - * Sets the value of the 'hostName' field. - * @param value the value to set. - */ - public void setHostName(java.lang.String value) { - this.hostName = value; - } - - /** - * Gets the value of the 'ipAddress' field. - * @return The value of the 'ipAddress' field. - */ - public java.lang.String getIpAddress() { - return ipAddress; - } - - - /** - * Sets the value of the 'ipAddress' field. - * @param value the value to set. - */ - public void setIpAddress(java.lang.String value) { - this.ipAddress = value; - } - - /** - * Creates a new ClientIdentifier RecordBuilder. - * @return A new ClientIdentifier RecordBuilder - */ - public static com.baeldung.apache.avro.generated.ClientIdentifier.Builder newBuilder() { - return new com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.ClientIdentifier.Builder newBuilder(com.baeldung.apache.avro.generated.ClientIdentifier.Builder other) { - if (other == null) { - return new com.baeldung.apache.avro.generated.ClientIdentifier.Builder(); - } else { - return new com.baeldung.apache.avro.generated.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 com.baeldung.apache.avro.generated.ClientIdentifier.Builder newBuilder(com.baeldung.apache.avro.generated.ClientIdentifier other) { - if (other == null) { - return new com.baeldung.apache.avro.generated.ClientIdentifier.Builder(); - } else { - return new com.baeldung.apache.avro.generated.ClientIdentifier.Builder(other); - } - } - - /** - * RecordBuilder for ClientIdentifier instances. - */ - @org.apache.avro.specific.AvroGenerated - public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase - implements org.apache.avro.data.RecordBuilder { - - private java.lang.String hostName; - private java.lang.String ipAddress; - - /** Creates a new Builder */ - private Builder() { - super(SCHEMA$, MODEL$); - } - - /** - * Creates a Builder by copying an existing Builder. - * @param other The existing Builder to copy. - */ - private Builder(com.baeldung.apache.avro.generated.ClientIdentifier.Builder other) { - super(other); - if (isValidValue(fields()[0], other.hostName)) { - this.hostName = data().deepCopy(fields()[0].schema(), other.hostName); - fieldSetFlags()[0] = other.fieldSetFlags()[0]; - } - if (isValidValue(fields()[1], other.ipAddress)) { - this.ipAddress = data().deepCopy(fields()[1].schema(), other.ipAddress); - fieldSetFlags()[1] = other.fieldSetFlags()[1]; - } - } - - /** - * Creates a Builder by copying an existing ClientIdentifier instance - * @param other The existing instance to copy. - */ - private Builder(com.baeldung.apache.avro.generated.ClientIdentifier other) { - super(SCHEMA$, MODEL$); - 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.String getHostName() { - return hostName; - } - - - /** - * Sets the value of the 'hostName' field. - * @param value The value of 'hostName'. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.ClientIdentifier.Builder setHostName(java.lang.String 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 com.baeldung.apache.avro.generated.ClientIdentifier.Builder clearHostName() { - hostName = null; - fieldSetFlags()[0] = false; - return this; - } - - /** - * Gets the value of the 'ipAddress' field. - * @return The value. - */ - public java.lang.String getIpAddress() { - return ipAddress; - } - - - /** - * Sets the value of the 'ipAddress' field. - * @param value The value of 'ipAddress'. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.ClientIdentifier.Builder setIpAddress(java.lang.String 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 com.baeldung.apache.avro.generated.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.String) defaultValue(fields()[0]); - record.ipAddress = fieldSetFlags()[1] ? this.ipAddress : (java.lang.String) defaultValue(fields()[1]); - return record; - } catch (org.apache.avro.AvroMissingFieldException e) { - throw e; - } 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)); - } - - @Override protected boolean hasCustomCoders() { return true; } - - @Override public void customEncode(org.apache.avro.io.Encoder out) - throws java.io.IOException - { - out.writeString(this.hostName); - - out.writeString(this.ipAddress); - - } - - @Override public void customDecode(org.apache.avro.io.ResolvingDecoder in) - throws java.io.IOException - { - org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff(); - if (fieldOrder == null) { - this.hostName = in.readString(); - - this.ipAddress = in.readString(); - - } else { - for (int i = 0; i < 2; i++) { - switch (fieldOrder[i].pos()) { - case 0: - this.hostName = in.readString(); - break; - - case 1: - this.ipAddress = in.readString(); - break; - - default: - throw new java.io.IOException("Corrupt ResolvingDecoder."); - } - } - } - } -} - - - - - - - - - - diff --git a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Parent.java b/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Parent.java deleted file mode 100644 index 4105cf4dec25..000000000000 --- a/apache-libraries/src/main/java/com/baeldung/apache/avro/generated/Parent.java +++ /dev/null @@ -1,362 +0,0 @@ -/** - * Autogenerated by Avro - * - * DO NOT EDIT DIRECTLY - */ -package com.baeldung.apache.avro.generated; - -import org.apache.avro.generic.GenericArray; -import org.apache.avro.specific.SpecificData; -import org.apache.avro.util.Utf8; -import org.apache.avro.message.BinaryMessageEncoder; -import org.apache.avro.message.BinaryMessageDecoder; -import org.apache.avro.message.SchemaStore; - -@org.apache.avro.specific.AvroGenerated -public class Parent extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord { - private static final long serialVersionUID = 2939149529655298213L; - - - public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"Parent\",\"namespace\":\"com.baeldung.apache.avro.generated\",\"fields\":[{\"name\":\"children\",\"type\":{\"type\":\"array\",\"items\":{\"type\":\"record\",\"name\":\"Child\",\"fields\":[{\"name\":\"name\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}]}}}]}"); - public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; } - - private static final SpecificData MODEL$ = new SpecificData(); - - private static final BinaryMessageEncoder ENCODER = - new BinaryMessageEncoder<>(MODEL$, SCHEMA$); - - private static final BinaryMessageDecoder DECODER = - new BinaryMessageDecoder<>(MODEL$, SCHEMA$); - - /** - * Return the BinaryMessageEncoder instance used by this class. - * @return the message encoder used by this class - */ - public static BinaryMessageEncoder getEncoder() { - return ENCODER; - } - - /** - * Return the BinaryMessageDecoder instance used by this class. - * @return the message decoder 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 - * @return a BinaryMessageDecoder instance for this class backed by the given SchemaStore - */ - public static BinaryMessageDecoder createDecoder(SchemaStore resolver) { - return new BinaryMessageDecoder<>(MODEL$, SCHEMA$, resolver); - } - - /** - * Serializes this Parent to a ByteBuffer. - * @return a buffer holding the serialized data for this instance - * @throws java.io.IOException if this instance could not be serialized - */ - public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException { - return ENCODER.encode(this); - } - - /** - * Deserializes a Parent from a ByteBuffer. - * @param b a byte buffer holding serialized data for an instance of this class - * @return a Parent instance decoded from the given buffer - * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class - */ - public static Parent fromByteBuffer( - java.nio.ByteBuffer b) throws java.io.IOException { - return DECODER.decode(b); - } - - private java.util.List children; - - /** - * 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 Parent() {} - - /** - * All-args constructor. - * @param children The new value for children - */ - public Parent(java.util.List children) { - this.children = children; - } - - @Override - public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; } - - @Override - public org.apache.avro.Schema getSchema() { return SCHEMA$; } - - // Used by DatumWriter. Applications should not call. - @Override - public java.lang.Object get(int field$) { - switch (field$) { - case 0: return children; - default: throw new IndexOutOfBoundsException("Invalid index: " + field$); - } - } - - // Used by DatumReader. Applications should not call. - @Override - @SuppressWarnings(value="unchecked") - public void put(int field$, java.lang.Object value$) { - switch (field$) { - case 0: children = (java.util.List)value$; break; - default: throw new IndexOutOfBoundsException("Invalid index: " + field$); - } - } - - /** - * Gets the value of the 'children' field. - * @return The value of the 'children' field. - */ - public java.util.List getChildren() { - return children; - } - - - /** - * Sets the value of the 'children' field. - * @param value the value to set. - */ - public void setChildren(java.util.List value) { - this.children = value; - } - - /** - * Creates a new Parent RecordBuilder. - * @return A new Parent RecordBuilder - */ - public static com.baeldung.apache.avro.generated.Parent.Builder newBuilder() { - return new com.baeldung.apache.avro.generated.Parent.Builder(); - } - - /** - * Creates a new Parent RecordBuilder by copying an existing Builder. - * @param other The existing builder to copy. - * @return A new Parent RecordBuilder - */ - public static com.baeldung.apache.avro.generated.Parent.Builder newBuilder(com.baeldung.apache.avro.generated.Parent.Builder other) { - if (other == null) { - return new com.baeldung.apache.avro.generated.Parent.Builder(); - } else { - return new com.baeldung.apache.avro.generated.Parent.Builder(other); - } - } - - /** - * Creates a new Parent RecordBuilder by copying an existing Parent instance. - * @param other The existing instance to copy. - * @return A new Parent RecordBuilder - */ - public static com.baeldung.apache.avro.generated.Parent.Builder newBuilder(com.baeldung.apache.avro.generated.Parent other) { - if (other == null) { - return new com.baeldung.apache.avro.generated.Parent.Builder(); - } else { - return new com.baeldung.apache.avro.generated.Parent.Builder(other); - } - } - - /** - * RecordBuilder for Parent instances. - */ - @org.apache.avro.specific.AvroGenerated - public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase - implements org.apache.avro.data.RecordBuilder { - - private java.util.List children; - - /** Creates a new Builder */ - private Builder() { - super(SCHEMA$, MODEL$); - } - - /** - * Creates a Builder by copying an existing Builder. - * @param other The existing Builder to copy. - */ - private Builder(com.baeldung.apache.avro.generated.Parent.Builder other) { - super(other); - if (isValidValue(fields()[0], other.children)) { - this.children = data().deepCopy(fields()[0].schema(), other.children); - fieldSetFlags()[0] = other.fieldSetFlags()[0]; - } - } - - /** - * Creates a Builder by copying an existing Parent instance - * @param other The existing instance to copy. - */ - private Builder(com.baeldung.apache.avro.generated.Parent other) { - super(SCHEMA$, MODEL$); - if (isValidValue(fields()[0], other.children)) { - this.children = data().deepCopy(fields()[0].schema(), other.children); - fieldSetFlags()[0] = true; - } - } - - /** - * Gets the value of the 'children' field. - * @return The value. - */ - public java.util.List getChildren() { - return children; - } - - - /** - * Sets the value of the 'children' field. - * @param value The value of 'children'. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.Parent.Builder setChildren(java.util.List value) { - validate(fields()[0], value); - this.children = value; - fieldSetFlags()[0] = true; - return this; - } - - /** - * Checks whether the 'children' field has been set. - * @return True if the 'children' field has been set, false otherwise. - */ - public boolean hasChildren() { - return fieldSetFlags()[0]; - } - - - /** - * Clears the value of the 'children' field. - * @return This builder. - */ - public com.baeldung.apache.avro.generated.Parent.Builder clearChildren() { - children = null; - fieldSetFlags()[0] = false; - return this; - } - - @Override - @SuppressWarnings("unchecked") - public Parent build() { - try { - Parent record = new Parent(); - record.children = fieldSetFlags()[0] ? this.children : (java.util.List) defaultValue(fields()[0]); - return record; - } catch (org.apache.avro.AvroMissingFieldException e) { - throw e; - } 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)); - } - - @Override protected boolean hasCustomCoders() { return true; } - - @Override public void customEncode(org.apache.avro.io.Encoder out) - throws java.io.IOException - { - long size0 = this.children.size(); - out.writeArrayStart(); - out.setItemCount(size0); - long actualSize0 = 0; - for (com.baeldung.apache.avro.generated.Child e0: this.children) { - actualSize0++; - out.startItem(); - e0.customEncode(out); - } - out.writeArrayEnd(); - if (actualSize0 != size0) - throw new java.util.ConcurrentModificationException("Array-size written was " + size0 + ", but element count was " + actualSize0 + "."); - - } - - @Override public void customDecode(org.apache.avro.io.ResolvingDecoder in) - throws java.io.IOException - { - org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff(); - if (fieldOrder == null) { - long size0 = in.readArrayStart(); - java.util.List a0 = this.children; - if (a0 == null) { - a0 = new SpecificData.Array((int)size0, SCHEMA$.getField("children").schema()); - this.children = a0; - } else a0.clear(); - SpecificData.Array ga0 = (a0 instanceof SpecificData.Array ? (SpecificData.Array)a0 : null); - for ( ; 0 < size0; size0 = in.arrayNext()) { - for ( ; size0 != 0; size0--) { - com.baeldung.apache.avro.generated.Child e0 = (ga0 != null ? ga0.peek() : null); - if (e0 == null) { - e0 = new com.baeldung.apache.avro.generated.Child(); - } - e0.customDecode(in); - a0.add(e0); - } - } - - } else { - for (int i = 0; i < 1; i++) { - switch (fieldOrder[i].pos()) { - case 0: - long size0 = in.readArrayStart(); - java.util.List a0 = this.children; - if (a0 == null) { - a0 = new SpecificData.Array((int)size0, SCHEMA$.getField("children").schema()); - this.children = a0; - } else a0.clear(); - SpecificData.Array ga0 = (a0 instanceof SpecificData.Array ? (SpecificData.Array)a0 : null); - for ( ; 0 < size0; size0 = in.arrayNext()) { - for ( ; size0 != 0; size0--) { - com.baeldung.apache.avro.generated.Child e0 = (ga0 != null ? ga0.peek() : null); - if (e0 == null) { - e0 = new com.baeldung.apache.avro.generated.Child(); - } - e0.customDecode(in); - a0.add(e0); - } - } - break; - - default: - throw new java.io.IOException("Corrupt ResolvingDecoder."); - } - } - } - } -} - - - - - - - - - - diff --git a/apache-libraries/src/main/java/com/baeldung/apache/avro/schemas/avroHttpRequest-schema.avsc b/apache-libraries/src/main/resources/schemas/avroHttpRequest-schema.avsc similarity index 100% rename from apache-libraries/src/main/java/com/baeldung/apache/avro/schemas/avroHttpRequest-schema.avsc rename to apache-libraries/src/main/resources/schemas/avroHttpRequest-schema.avsc diff --git a/apache-libraries/src/main/java/com/baeldung/apache/avro/schemas/parent.avsc b/apache-libraries/src/main/resources/schemas/parent.avsc similarity index 100% rename from apache-libraries/src/main/java/com/baeldung/apache/avro/schemas/parent.avsc rename to apache-libraries/src/main/resources/schemas/parent.avsc diff --git a/apache-libraries/src/test/java/com/baeldung/apache/avro/storingnullvaluesinavrofile/AvroUserUnitTest.java b/apache-libraries/src/test/java/com/baeldung/apache/avro/storingnullvaluesinavrofile/AvroUserUnitTest.java new file mode 100644 index 000000000000..dfc0fcb6c229 --- /dev/null +++ b/apache-libraries/src/test/java/com/baeldung/apache/avro/storingnullvaluesinavrofile/AvroUserUnitTest.java @@ -0,0 +1,78 @@ +package com.baeldung.apache.avro.storingnullvaluesinavrofile; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.nio.file.Path; + +import org.apache.avro.Schema; +import org.apache.avro.generic.GenericRecord; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +class AvroUserUnitTest { + + private AvroUser user; + private Schema schema; + + @BeforeEach + void setUp() { + user = new AvroUser(); + user.setId(1998983092L); + user.setName("Test Name 1"); + user.setActive(true); // Using @Nullable + user.setLastUpdatedBy("yesterday"); // Using union type + user.setEmail("emailAddress"); // Using optional field + } + + @Test + void whenSerializingUserWithNullPropFromStringSchema_thenSuccess(@TempDir Path tempDir) { + user.setLastUpdatedBy(null); + schema = AvroUser.createSchemaWithOptionalFields1(); + + String filePath = tempDir.resolve("test.avro").toString(); + GenericRecord record = AvroUser.createRecord(AvroUser.createSchemaFromJson(), user); + + assertDoesNotThrow(() -> AvroUser.writeToAvroFile(schema, record, filePath)); + + File avroFile = new File(filePath); + assertTrue(avroFile.exists()); + assertTrue(avroFile.length() > 0); + } + + @Test + void givenSchemaBuilderWithOptionalFields1_whenCreatingSchema_thenSupportsNull(@TempDir Path tempDir) { + user.setLastUpdatedBy(null); + String filePath = tempDir.resolve("test.avro").toString(); + + schema = AvroUser.createSchemaWithOptionalFields1(); + GenericRecord record = AvroUser.createRecord(schema, user); + + assertTrue(schema.getField("lastUpdatedBy").schema().isNullable(), + "Union type field should be nullable"); + assertDoesNotThrow(() -> AvroUser.writeToAvroFile(schema, record, filePath)); + + File avroFile = new File(filePath); + assertTrue(avroFile.exists()); + assertTrue(avroFile.length() > 0); + } + + @Test + void givenSchemaBuilderWithOptionalFields2_whenCreatingSchema_thenSupportsNull(@TempDir Path tempDir) { + user.setEmail(null); + String filePath = tempDir.resolve("test.avro").toString(); + + schema = AvroUser.createSchemaWithOptionalFields2(); + GenericRecord record = AvroUser.createRecord(schema, user); + + assertTrue(schema.getField("email").schema().isNullable(), + "Union type field should be nullable"); + assertDoesNotThrow(() -> AvroUser.writeToAvroFile(schema, record, filePath)); + + File avroFile = new File(filePath); + assertTrue(avroFile.exists()); + assertTrue(avroFile.length() > 0); + } +}