org.apache.avro.file

## Class DataFileReader<D>

- java.lang.Object
- - org.apache.avro.file.DataFileStream<D>
  - - org.apache.avro.file.DataFileReader<D>

- All Implemented Interfaces:
    Closeable, AutoCloseable, Iterable<D>, Iterator<D>, FileReader<D>

      public class DataFileReader<D>
      extends DataFileStream<D>
      implements FileReader<D>

  Random access to files written with `DataFileWriter`.
  See Also:
    `DataFileWriter`

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.apache.avro.file.DataFileStream

      `DataFileStream.Header`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `` | `DataFileReader(File file, DatumReader<D> reader)` Construct a reader for a file.
    `` | `DataFileReader(SeekableInput sin, DatumReader<D> reader)` Construct a reader for a file.
    `protected` | `DataFileReader(SeekableInput sin, DatumReader<D> reader, boolean closeOnError, byte[] magic)` Construct a reader for a file.
    `protected` | `DataFileReader(SeekableInput sin, DatumReader<D> reader, DataFileStream.Header header)` Construct using a `DataFileStream.Header`.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `blockFinished()`
    `static <D> FileReader<D>` | `openReader(File file, DatumReader<D> reader)` Open a reader for a file.
    `static <D> FileReader<D>` | `openReader(SeekableInput in, DatumReader<D> reader)` Open a reader for a file.
    `static <D> DataFileReader<D>` | `openReader(SeekableInput in, DatumReader<D> reader, DataFileStream.Header header, boolean sync)` Construct a reader for a file at the current position of the input, without reading the header.
    `boolean` | `pastSync(long position)` Return true if past the next synchronization point after a position.
    `long` | `previousSync()` Return the last synchronization point before our current position.
    `void` | `seek(long position)` Move to a specific, known synchronization point, one returned from `DataFileWriter.sync()` while writing.
    `void` | `sync(long position)` Move to the next synchronization point after a position.
    `long` | `tell()` Return the current position in the input.

    - ### Methods inherited from class org.apache.avro.file.DataFileStream

      `close, getBlockCount, getBlockSize, getHeader, getMeta, getMetaKeys, getMetaLong, getMetaString, getSchema, hasNext, iterator, next, next, nextBlock, remove`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.apache.avro.file.FileReader

      `getSchema, next`

    - ### Methods inherited from interface java.util.Iterator

      `forEachRemaining, hasNext, next, remove`

    - ### Methods inherited from interface java.lang.Iterable

      `forEach, iterator, spliterator`

    - ### Methods inherited from interface java.io.Closeable

      `close`

- - ### Constructor Detail

    - #### DataFileReader

          public DataFileReader(File file,
                                DatumReader<D> reader)
                         throws IOException

      Construct a reader for a file. For example,if you want to read a file record,you need to close the resource. You can use try-with-resource as follows:

           try (FileReader dataFileReader =
           DataFileReader.openReader(file,datumReader)) { //Consume the reader } catch
           (IOException e) { throw new RunTimeIOException(e,"Failed to read metadata for
           file: %s", file); }

      Throws:
        `IOException`

    - #### DataFileReader

          public DataFileReader(SeekableInput sin,
                                DatumReader<D> reader)
                         throws IOException

      Construct a reader for a file. For example,if you want to read a file record,you need to close the resource. You can use try-with-resource as follows:

           try (FileReader dataFileReader =
           DataFileReader.openReader(file,datumReader)) { //Consume the reader } catch
           (IOException e) { throw new RunTimeIOException(e,"Failed to read metadata for
           file: %s", file); }

      Throws:
        `IOException`

    - #### DataFileReader

          protected DataFileReader(SeekableInput sin,
                                   DatumReader<D> reader,
                                   boolean closeOnError,
                                   byte[] magic)
                            throws IOException

      Construct a reader for a file. Please close resource files yourself.
      Throws:
        `IOException`

    - #### DataFileReader

          protected DataFileReader(SeekableInput sin,
                                   DatumReader<D> reader,
                                   DataFileStream.Header header)
                            throws IOException

      Construct using a `DataFileStream.Header`. Does not call `sync(long)` or `seek(long)`.
      Throws:
        `IOException`

  - ### Method Detail

    - #### openReader

          public static <D> FileReader<D> openReader(File file,
                                                     DatumReader<D> reader)
                                              throws IOException

      Open a reader for a file.
      Throws:
        `IOException`

    - #### openReader

          public static <D> FileReader<D> openReader(SeekableInput in,
                                                     DatumReader<D> reader)
                                              throws IOException

      Open a reader for a file.
      Throws:
        `IOException`

    - #### openReader

          public static <D> DataFileReader<D> openReader(SeekableInput in,
                                                         DatumReader<D> reader,
                                                         DataFileStream.Header header,
                                                         boolean sync)
                                                  throws IOException

      Construct a reader for a file at the current position of the input, without reading the header.
      Parameters:
        `sync` - True to read forward to the next sync point after opening, false to assume that the input is already at a valid sync point.
      Throws:
        `IOException`

    - #### seek

          public void seek(long position)
                    throws IOException

      Move to a specific, known synchronization point, one returned from `DataFileWriter.sync()` while writing. If synchronization points were not saved while writing a file, use `sync(long)` instead.
      Throws:
        `IOException`

    - #### sync

          public void sync(long position)
                    throws IOException

      Move to the next synchronization point after a position. To process a range of file entires, call this with the starting position, then check `pastSync(long)` with the end point before each call to `DataFileStream.next()`.
      Specified by:
        `sync` in interface `FileReader<D>`
      Throws:
        `IOException`

    - #### blockFinished

          protected void blockFinished()
                                throws IOException

      Overrides:
        `blockFinished` in class `DataFileStream<D>`
      Throws:
        `IOException`

    - #### previousSync

          public long previousSync()

      Return the last synchronization point before our current position.

    - #### pastSync

          public boolean pastSync(long position)
                           throws IOException

      Return true if past the next synchronization point after a position.
      Specified by:
        `pastSync` in interface `FileReader<D>`
      Throws:
        `IOException`

    - #### tell

          public long tell()
                    throws IOException

      Description copied from interface: `FileReader`
      Return the current position in the input.
      Specified by:
        `tell` in interface `FileReader<D>`
      Throws:
        `IOException`

Copyright © 2009–2024 The Apache Software Foundation. All rights reserved.