You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary Gregory (JIRA)" <ji...@apache.org> on 2014/04/04 22:49:15 UTC

[jira] [Commented] (CSV-110) Add ability to parse single lines

    [ https://issues.apache.org/jira/browse/CSV-110?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13960407#comment-13960407 ] 

Gary Gregory commented on CSV-110:
----------------------------------

Hello Gabriel:

I think the API can take care of simple cases like this already. Please the unit test in {{org.apache.commons.csv.CSVParserTest}} I just added to trunk.

It seems to me that {{parser.getRecords().get(0);}} is sweet and short:

{code:java}
    @Test
    public void testGetOneLine() throws IOException {
        final CSVParser parser = CSVParser.parse(CSV_INPUT_1, CSVFormat.DEFAULT);
        final CSVRecord record = parser.getRecords().get(0);
        assertArrayEquals(RESULT[0], record.values());
        parser.close();
    }
{code}

If you want to use the Deque's {{getFirst()}} API, you can now do so:

{code:java}
    @Test
    public void testGetOneLineCustomCollection() throws IOException {
        final CSVParser parser = CSVParser.parse(CSV_INPUT_1, CSVFormat.DEFAULT);
        final CSVRecord record = parser.getRecords(new LinkedList<CSVRecord>()).getFirst();
        assertArrayEquals(RESULT[0], record.values());
        parser.close();
    }
{code}

> Add ability to parse single lines
> ---------------------------------
>
>                 Key: CSV-110
>                 URL: https://issues.apache.org/jira/browse/CSV-110
>             Project: Commons CSV
>          Issue Type: New Feature
>            Reporter: Gabriel Reid
>         Attachments: CSV-110.patch
>
>
> Due to the iterator-based API of CSVParser, there is currently no simple and convenient way to parse single lines of CSV-formatted data. The intention of this ticket is to add something along the lines of the following:
> {code}
> CSVLineParser lineParser = new CSVLineParser(csvFormat);
> String singleLine = "a,b,c";
> CSVRecord singleRecord lineParser.parseLine(singleLine);
> {code}
> The use case of parsing single lines comes up very often in terms of distributed batch processing scenarios (i.e. Hadoop jobs), and CSV-style formats are also regularly used in such scenarios. Currently, projects are often forced to build their own ad-hoc CSV parsing solutions, so adding the ability to parse single lines to commons-csv would be very useful to these projects, as well as anyone doing parsing based on input that isn't necessary in the form of a single stream.



--
This message was sent by Atlassian JIRA
(v6.2#6252)