Package org.apache.avro.io
Class ResolvingDecoder
java.lang.Object
org.apache.avro.io.Decoder
org.apache.avro.io.ParsingDecoder
org.apache.avro.io.ValidatingDecoder
org.apache.avro.io.ResolvingDecoder
- All Implemented Interfaces:
Parser.ActionHandler,SkipParser.SkipHandler
Decoder that performs type-resolution between the reader's and
writer's schemas.
When resolving schemas, this class will return the values of fields in
_writer's_ order, not the reader's order. (However, it returns _only_ the
reader's fields, not any extra fields the writer may have written.) To help
clients handle fields that appear to be coming out of order, this class
defines the method readFieldOrder().
See the parser documentation for information on how this works.
-
Field Summary
Fields inherited from class org.apache.avro.io.ValidatingDecoder
inFields inherited from class org.apache.avro.io.ParsingDecoder
parser -
Method Summary
Modifier and TypeMethodDescriptionHandle the action symbol top when the input is sought to be taken off the stack.final voiddrain()Consume any more data that has been written by the writer but not needed by the reader so that the the underlying decoder is in proper shape for the next record.readBytes(ByteBuffer old) Reads a byte-string written byEncoder.writeBytes(java.nio.ByteBuffer). if old is not null and has sufficient capacity to take in the bytes being read, the bytes are returned in old.doubleReads a double written byEncoder.writeDouble(double).intreadEnum()Reads an enumeration.final Schema.Field[]Returns the actual order in which the reader's fields will be returned to the reader.final Schema.Field[]Same asreadFieldOrder()except that it returns null if there was no reordering of fields, i.e., if the correct thing for the reader to do is to read (all) of its fields in the order specified by its own schema (useful for optimizations).floatReads a float written byEncoder.writeFloat(float).intReads the tag of a union written byEncoder.writeIndex(int).longreadLong()Reads a long written byEncoder.writeLong(long).Reads a char-string written byEncoder.writeString(org.apache.avro.util.Utf8).readString(Utf8 old) Reads a char-string written byEncoder.writeString(org.apache.avro.util.Utf8).static ObjectProduces an opaque resolver that can be used to construct a newResolvingDecoder(Object, Decoder).voidSkips the action at the top of the stack.voidDiscards a byte-string written byEncoder.writeBytes(java.nio.ByteBuffer).voidDiscards a char-string written byEncoder.writeString(org.apache.avro.util.Utf8).Methods inherited from class org.apache.avro.io.ValidatingDecoder
arrayNext, configure, mapNext, readArrayStart, readBoolean, readFixed, readInt, readMapStart, readNull, skipArray, skipFixed, skipFixed, skipMapMethods inherited from class org.apache.avro.io.ParsingDecoder
skipTopSymbol
-
Method Details
-
resolve
Produces an opaque resolver that can be used to construct a newResolvingDecoder(Object, Decoder). The returned Object is immutable and hence can be simultaneously used in many ResolvingDecoders. This method is reasonably expensive, the users are encouraged to cache the result.- Parameters:
writer- The writer's schema. Cannot be null.reader- The reader's schema. Cannot be null.- Returns:
- The opaque resolver.
- Throws:
IOExceptionNullPointerException- ifwriterorreaderisnull
-
readFieldOrder
Returns the actual order in which the reader's fields will be returned to the reader. This method is useful becauseResolvingDecoderreturns values in the order written by the writer, rather than the order expected by the reader. This method allows readers to figure out what fields to expect. Let's say the reader is expecting a three-field record, the first field is a long, the second a string, and the third an array. In this case, a typical usage might be as follows:Schema.Fields[] fieldOrder = in.readFieldOrder(); for (int i = 0; i < 3; i++) { switch (fieldOrder[i].pos()) { case 1: foo(in.readLong()); break; case 2: someVariable = in.readString(); break; case 3: bar(in); // The code of "bar" will read an array-of-int break; }Note thatResolvingDecoderwill return only the fields expected by the reader, not other fields that may have been written by the writer. Thus, the iteration-count of "3" in the above loop will always be correct. Throws a runtime exception if we're not just about to read the first field of a record. (If the client knows the order of incoming fields, then the client does not need to call this method but rather can just start reading the field values.)- Throws:
AvroTypeException- If we're not starting a new recordIOException
-
readFieldOrderIfDiff
Same asreadFieldOrder()except that it returns null if there was no reordering of fields, i.e., if the correct thing for the reader to do is to read (all) of its fields in the order specified by its own schema (useful for optimizations).- Throws:
IOException
-
drain
Consume any more data that has been written by the writer but not needed by the reader so that the the underlying decoder is in proper shape for the next record. This situation happens when, for example, the writer writes a record with two fields and the reader needs only the first field. This function should be called after completely decoding an object but before next object can be decoded from the same underlying decoder either directly or through another resolving decoder. If the same resolving decoder is used for the next object as well, calling this method is optional; the state of this resolving decoder ensures that any leftover portions are consumed before the next object is decoded.- Throws:
IOException
-
readLong
Description copied from class:DecoderReads a long written byEncoder.writeLong(long).- Overrides:
readLongin classValidatingDecoder- Throws:
IOException
-
readFloat
Description copied from class:DecoderReads a float written byEncoder.writeFloat(float).- Overrides:
readFloatin classValidatingDecoder- Throws:
IOException
-
readDouble
Description copied from class:DecoderReads a double written byEncoder.writeDouble(double).- Overrides:
readDoublein classValidatingDecoder- Throws:
IOException
-
readString
Description copied from class:DecoderReads a char-string written byEncoder.writeString(org.apache.avro.util.Utf8).- Overrides:
readStringin classValidatingDecoder- Throws:
IOException
-
readString
Description copied from class:DecoderReads a char-string written byEncoder.writeString(org.apache.avro.util.Utf8).- Overrides:
readStringin classValidatingDecoder- Throws:
IOException
-
skipString
Description copied from class:DecoderDiscards a char-string written byEncoder.writeString(org.apache.avro.util.Utf8).- Overrides:
skipStringin classValidatingDecoder- Throws:
IOException
-
readBytes
Description copied from class:DecoderReads a byte-string written byEncoder.writeBytes(java.nio.ByteBuffer). if old is not null and has sufficient capacity to take in the bytes being read, the bytes are returned in old.- Overrides:
readBytesin classValidatingDecoder- Throws:
IOException
-
skipBytes
Description copied from class:DecoderDiscards a byte-string written byEncoder.writeBytes(java.nio.ByteBuffer).- Overrides:
skipBytesin classValidatingDecoder- Throws:
IOException
-
readEnum
Description copied from class:DecoderReads an enumeration.- Overrides:
readEnumin classValidatingDecoder- Returns:
- The enumeration's value.
- Throws:
IOException
-
readIndex
Description copied from class:DecoderReads the tag of a union written byEncoder.writeIndex(int).- Overrides:
readIndexin classValidatingDecoder- Throws:
IOException
-
doAction
Description copied from interface:Parser.ActionHandlerHandle the action symbol top when the input is sought to be taken off the stack.- Specified by:
doActionin interfaceParser.ActionHandler- Overrides:
doActionin classValidatingDecoder- Parameters:
input- The input symbol from the caller of advancetop- The symbol at the top the stack.- Returns:
- null if advance() is to continue processing the stack. If not null the return value will be returned by advance().
- Throws:
IOException
-
skipAction
Description copied from interface:SkipParser.SkipHandlerSkips the action at the top of the stack.- Specified by:
skipActionin interfaceSkipParser.SkipHandler- Overrides:
skipActionin classParsingDecoder- Throws:
IOException
-