Class AvroSource.AvroReader<T>
- Type Parameters:
T- The type of records contained in the block.
- All Implemented Interfaces:
AutoCloseable
- Enclosing class:
AvroSource<T>
BlockBasedSource.BlockBasedReader for reading blocks from Avro files.
An Avro Object Container File consists of a header followed by a 16-bit sync marker and then a sequence of blocks, where each block begins with two encoded longs representing the total number of records in the block and the block's size in bytes, followed by the block's (optionally-encoded) records. Each block is terminated by a 16-bit sync marker.
-
Field Summary
Fields inherited from class org.apache.beam.sdk.io.BoundedSource.BoundedReader
SPLIT_POINTS_UNKNOWN -
Constructor Summary
ConstructorsConstructorDescriptionAvroReader(AvroSource<T> source) Reads Avro records of typeTfrom the specified source. -
Method Summary
Modifier and TypeMethodDescriptionorg.apache.beam.sdk.extensions.avro.io.AvroSource.AvroBlock<T> Returns the current block (the block that was read by the last successful call toBlockBasedSource.BlockBasedReader.readNextBlock()).longReturns the largest offset such that starting to read from that offset includes the current block.longReturns the size of the current block in bytes as it is represented in the underlying file, if possible.Returns aSourcedescribing the same input that thisReadercurrently reads (including items already read).longReturns the total amount of parallelism in the unprocessed part of this reader's currentBoundedSource(as would be returned byBoundedSource.BoundedReader.getCurrentSource()).booleanRead the next block from the input.protected voidstartReading(ReadableByteChannel channel) Performs any initialization of the subclass ofFileBasedReaderthat involves IO operations.Methods inherited from class org.apache.beam.sdk.io.BlockBasedSource.BlockBasedReader
getCurrent, getCurrentOffset, getFractionConsumed, isAtSplitPoint, readNextRecordMethods inherited from class org.apache.beam.sdk.io.FileBasedSource.FileBasedReader
advanceImpl, allowsDynamicSplitting, close, startImplMethods inherited from class org.apache.beam.sdk.io.OffsetBasedSource.OffsetBasedReader
advance, getSplitPointsConsumed, isDone, isStarted, splitAtFraction, startMethods inherited from class org.apache.beam.sdk.io.BoundedSource.BoundedReader
getCurrentTimestamp
-
Constructor Details
-
AvroReader
Reads Avro records of typeTfrom the specified source.
-
-
Method Details
-
getCurrentSource
Description copied from class:BoundedSource.BoundedReaderReturns aSourcedescribing the same input that thisReadercurrently reads (including items already read).Usage
Reader subclasses can use this method for convenience to access unchanging properties of the source being read. Alternatively, they can cache these properties in the constructor.
The framework will call this method in the course of dynamic work rebalancing, e.g. after a successful
BoundedSource.BoundedReader.splitAtFraction(double)call.Mutability and thread safety
Remember that
Sourceobjects must always be immutable. However, the return value of this function may be affected by dynamic work rebalancing, happening asynchronously viaBoundedSource.BoundedReader.splitAtFraction(double), meaning it can return a differentSourceobject. However, the returned object itself will still itself be immutable. Callers must take care not to rely on properties of the returned source that may be asynchronously changed as a result of this process (e.g. do not cache an end offset when reading a file).Implementation
For convenience, subclasses should usually return the most concrete subclass of
Sourcepossible. In practice, the implementation of this method should nearly always be one of the following:- Source that inherits from a base class that already implements
BoundedSource.BoundedReader.getCurrentSource(): delegate to base class. In this case, it is almost always an error for the subclass to maintain its own copy of the source.public FooReader(FooSource<T> source) { super(source); } public FooSource<T> getCurrentSource() { return (FooSource<T>)super.getCurrentSource(); } - Source that does not support dynamic work rebalancing: return a private final variable.
private final FooSource<T> source; public FooReader(FooSource<T> source) { this.source = source; } public FooSource<T> getCurrentSource() { return source; } BoundedSource.BoundedReaderthat explicitly supports dynamic work rebalancing: maintain a variable pointing to an immutable source object, and protect it with synchronization.private FooSource<T> source; public FooReader(FooSource<T> source) { this.source = source; } public synchronized FooSource<T> getCurrentSource() { return source; } public synchronized FooSource<T> splitAtFraction(double fraction) { ... FooSource<T> primary = ...; FooSource<T> residual = ...; this.source = primary; return residual; }
- Overrides:
getCurrentSourcein classFileBasedSource.FileBasedReader<T>
- Source that inherits from a base class that already implements
-
readNextBlock
public boolean readNextBlock()Description copied from class:BlockBasedSource.BlockBasedReaderRead the next block from the input.- Specified by:
readNextBlockin classBlockBasedSource.BlockBasedReader<T>
-
getCurrentBlock
Description copied from class:BlockBasedSource.BlockBasedReaderReturns the current block (the block that was read by the last successful call toBlockBasedSource.BlockBasedReader.readNextBlock()). May return null initially, or if no block has been successfully read.- Specified by:
getCurrentBlockin classBlockBasedSource.BlockBasedReader<T>
-
getCurrentBlockOffset
public long getCurrentBlockOffset()Description copied from class:BlockBasedSource.BlockBasedReaderReturns the largest offset such that starting to read from that offset includes the current block.- Specified by:
getCurrentBlockOffsetin classBlockBasedSource.BlockBasedReader<T>
-
getCurrentBlockSize
public long getCurrentBlockSize()Description copied from class:BlockBasedSource.BlockBasedReaderReturns the size of the current block in bytes as it is represented in the underlying file, if possible. This method may return0if the size of the current block is unknown.The size returned by this method must be such that for two successive blocks A and B,
offset(A) + size(A) <= offset(B). If this is not satisfied, the progress reported by theBlockBasedReaderwill be non-monotonic and will interfere with the quality (but not correctness) of dynamic work rebalancing.This method and
BlockBasedSource.Block.getFractionOfBlockConsumed()are used to provide an estimate of progress within a block (getCurrentBlock().getFractionOfBlockConsumed() * getCurrentBlockSize()). It is acceptable for the result of this computation to be0, but progress estimation will be inaccurate.- Specified by:
getCurrentBlockSizein classBlockBasedSource.BlockBasedReader<T>
-
getSplitPointsRemaining
public long getSplitPointsRemaining()Description copied from class:BoundedSource.BoundedReaderReturns the total amount of parallelism in the unprocessed part of this reader's currentBoundedSource(as would be returned byBoundedSource.BoundedReader.getCurrentSource()). This corresponds to all unprocessed split point records (seeRangeTracker), including the last split point returned, in the remainder part of the source.This function should be implemented only in addition to
BoundedSource.BoundedReader.getSplitPointsConsumed()and only if an exact value can be returned.Consider the following examples: (1) An input that can be read in parallel down to the individual records, such as
CountingSource.upTo(long), is called "perfectly splittable". (2) a "block-compressed" file format such as, in which a block of records has to be read as a whole, but different blocks can be read in parallel. (3) An "unsplittable" input such as a cursor in a database.invalid reference
AvroIOAssume for examples (1) and (2) that the number of records or blocks remaining is known:
- Any
readerfor which the last call toSource.Reader.start()orSource.Reader.advance()has returned true should should not return 0, because this reader itself represents parallelism at least 1. This condition holds independent of whether the input is splittable. - A finished reader (for which
Source.Reader.start()orSource.Reader.advance()) has returned false should return a value of 0. This condition holds independent of whether the input is splittable. - For example 1: After returning record #30 (starting at 1) out of 50 in a perfectly splittable 50-record input, this value should be 21 (20 remaining + 1 current) if the total number of records is known.
- For example 2: After returning a record in block 3 in a block-compressed file consisting of 5 blocks, this value should be 3 (since blocks 4 and 5 can be processed in parallel by new readers produced via dynamic work rebalancing, while the current reader continues processing block 3) if the total number of blocks is known.
- For example (3): a reader for any non-empty unsplittable input, should return 1 until it is finished, at which point it should return 0.
- For any reader: After returning the last split point in a file (e.g., the last record in example (1), the first record in the last block for example (2), or the first record in the file for example (3), this value should be 1: apart from the current task, no additional remainder can be split off.
Defaults to
BoundedSource.BoundedReader.SPLIT_POINTS_UNKNOWN. Any value less than 0 will be interpreted as unknown.Thread safety
See the javadoc onBoundedSource.BoundedReaderfor information about thread safety.- Overrides:
getSplitPointsRemainingin classOffsetBasedSource.OffsetBasedReader<T>- See Also:
- Any
-
startReading
Description copied from class:FileBasedSource.FileBasedReaderPerforms any initialization of the subclass ofFileBasedReaderthat involves IO operations. Will only be invoked once and before that invocation the base class will seek the channel to the source's starting offset.Provided
ReadableByteChannelis for the file represented by the source of this reader. Subclass may use thechannelto build a higher level IO abstraction, e.g., a BufferedReader or an XML parser.If the corresponding source is for a subrange of a file,
channelis guaranteed to be an instance of the typeSeekableByteChannel.After this method is invoked the base class will not be reading data from the channel or adjusting the position of the channel. But the base class is responsible for properly closing the channel.
- Specified by:
startReadingin classFileBasedSource.FileBasedReader<T>- Parameters:
channel- a byte channel representing the file backing the reader.- Throws:
IOException
-