For the complete documentation index, see llms.txt. This page is also available as Markdown.

Record Readers

RecordReader implementations — CSV, JSON, image, regex, line, and custom readers

A RecordReader is the entry point for data into DataVec. It reads raw bytes from an InputSplit and converts them into List<Writable> records — one list per data example, where each element corresponds to a column in your Schema.

The RecordReader Interface

Every reader implements RecordReader and provides:

Method
Description

initialize(InputSplit split)

Set up the reader against a data source

initialize(Configuration conf, InputSplit split)

Set up with additional configuration

hasNext()

True if another record is available

next()

Return the next record as List<Writable>

nextRecord()

Return the next Record with optional RecordMetaData

reset()

Restart iteration from the beginning

close()

Release resources

After calling initialize, use hasNext / next in a loop, or pass the reader directly to a DataSetIterator.

InputSplit

An InputSplit tells the reader where to find data. The main implementations:

FileSplit

Points to a directory or single file. By default, all files recursively under the directory are included.

// All files under a directory
InputSplit split = new FileSplit(new File("/data/train/"));

// Only CSV files, shuffled
InputSplit split = new FileSplit(
    new File("/data/train/"),
    new String[]{"csv"},
    new Random(42)
);

// A single file
InputSplit split = new FileSplit(new File("/data/train.csv"));

NumberedFileInputSplit

For files named with sequential numbers in a format string:

CollectionInputSplit

For an explicit list of URIs:

InputStreamInputSplit

For streaming data from any InputStream:

CSV Readers

CSVRecordReader

The most commonly used reader. Reads a CSV (or TSV, or any delimiter-separated) file line by line, producing one List<Writable> per line.

All values are returned as Text (string) Writable objects. Numeric conversion happens automatically in the TransformProcess or during DataSetIterator construction.

When your CSV has mixed quoted fields:

CSVSequenceRecordReader

Reads multiple files, treating each file as one sequence. Each line in a file is one time step; each value in a line is one feature at that time step.

This reader implements SequenceRecordReader, so use it with SequenceRecordReaderDataSetIterator.

CSVRegexRecordReader

Splits columns using regex patterns rather than a simple delimiter. Useful for CSV files with inconsistent spacing or mixed delimiters.

CSVVariableSlidingWindowRecordReader

Reads an entire CSV and produces subsequences using a variable sliding window. The window starts at size 1, grows to maxLinesPerSequence, then shrinks back. Useful for training on all possible subsequences of a dataset.

Text Readers

LineRecordReader

Reads a file line by line. Each line becomes a single-element record containing a Text writable. No parsing is done — you receive the raw line. Useful when you want to apply your own parsing in a TransformProcess or custom transform.

RegexLineRecordReader

Reads a file line by line and splits each line into fields using a regex with capture groups. Each capture group becomes one Text writable in the record.

Lines that do not match the regex result in an exception by default.

RegexSequenceRecordReader

Like RegexLineRecordReader, but reads an entire file as a sequence, with one time step per line. Supports three invalid-line handling modes:

  • FailOnInvalid — throw an exception (default)

  • SkipInvalid — silently skip non-matching lines

  • SkipInvalidWithWarning — skip but log a warning

ListStringRecordReader

Reads from an in-memory list of strings. Each string is parsed as a single-column record. Useful for testing or when you have already loaded text into memory.

JSON / XML / YAML Readers

JacksonRecordReader

Reads JSON, XML, or YAML files using Jackson. Each file (or each element in an array) becomes one record. You specify a FieldSelection to pull out the fields you need.

For XML, replace new ObjectMapper() with new XmlMapper() from the Jackson XML module.

Image Reader

ImageRecordReader

Reads a directory of images, where each subdirectory is treated as a class label (one-of-K labeling). All images are resized to the specified height, width, and channel count.

Expected directory structure:

With this structure, images in cat/ get label index 0 and images in dog/ get label index 1 (alphabetical ordering).

For image augmentation and transforms, see Image Data.

File Reader

FileRecordReader

Reads individual files, returning the file path as a Text writable and the label derived from the parent directory name. Most commonly used as a base class rather than directly.

Sparse Format Readers

LibSvmRecordReader and SVMLightRecordReader

These readers parse sparse feature formats widely used in linear model and kernel method communities. The format encodes each example as:

Zero-valued features are omitted. LibSvmRecordReader is a subclass of SVMLightRecordReader with minor format differences.

Collection Readers

CollectionRecordReader

Wraps an in-memory List<List<Writable>> as a reader. Primarily used in unit tests.

CollectionSequenceRecordReader

Like CollectionRecordReader but for sequence data: wraps List<List<List<Writable>>>.

Combining Readers

ConcatenatingRecordReader

Chains multiple readers sequentially. When the first reader is exhausted, reading continues with the second, and so on. Useful for combining training files across multiple directories.

TransformProcessRecordReader

Wraps another reader and applies a TransformProcess to every record before returning it. Useful when you want to inline transformation without a separate executor step.

For sequence readers, use TransformProcessSequenceRecordReader instead.

Adding Listeners

You can attach a RecordListener to any reader for debugging or monitoring:

Custom listeners implement the RecordListener interface:

Choosing the Right Reader

Your data
Use

CSV or TSV files

CSVRecordReader

One sequence per CSV file

CSVSequenceRecordReader

JSON / XML / YAML files

JacksonRecordReader

Log files with structured format

RegexLineRecordReader (single record per line) or RegexSequenceRecordReader (whole file as sequence)

Labeled image directories

ImageRecordReader

Sparse feature vectors

LibSvmRecordReader / SVMLightRecordReader

In-memory data (testing)

CollectionRecordReader

Multiple files to concatenate

ConcatenatingRecordReader

Any reader + inline transforms

TransformProcessRecordReader

Last updated

Was this helpful?