Skip to content

Extract only certain columns of a csv line #166

Description

@kodyrecords

I would be great to configure the reader to only read certain columns (indexes) of a csv file.

Imagine csv lines with like 150 columns in a row. But you are only interested in 20 of those values.
On large data sets, this makes a huge difference to not parse just all fields of a line into the CsvRecord.

Example of extracting just 2 values of each row.
It would be nice to provide some kind of "tokens-map" for the reader, like:

Map<Integer, CsvField> tokens = Map.of(
    5, CsvField.DEPARTURE
    8, CsvField.ARRIVAL
);

CsvField could best be an Enum, or as an alternative static String constants.

The reader would then have to keep those values in some kind of a reverse map, like Map<CsvField, Object>.
So the user is then able to fetch the desired values using the CsvField as key (instead of indexes access).

Usage:

                CsvReader<CsvRecord> reader = CsvReader.builder()
                                 .withTokens(tokens) //or similar
                                 .ofCsvRecord(file);

                reader.stream().forEach(record -> {
                       //consume the results by enums or static strings
                       record.getField(CsvField.DEPARTURE); //value at initial index 5
                       record.getField(CsvField.ARRIVAL); //value at initial index 8
                });

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions