Example usage for org.apache.commons.csv CSVParser getRecords

List of usage examples for org.apache.commons.csv CSVParser getRecords

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVParser getRecords.

Prototype

public List<CSVRecord> getRecords() throws IOException 

Source Link

Document

Parses the CSV input according to the given format and returns the content as a list of CSVRecord CSVRecords .

Usage

From source file:com.miovision.oss.awsbillingtools.parser.DetailedLineItemTest.java

private DetailedLineItem givenDetailedLineItemWithTags() {
    try {//from   www. j av  a2 s.  c  om
        CSVParser csvParser = CSVFormat.EXCEL.parse(new StringReader(TEST_RECORD_WITH_TAGS));
        CSVRecord csvRecord = csvParser.getRecords().get(0);
        return givenDetailedLineItem(csvRecord, Arrays.asList("foo"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.miovision.oss.awsbillingtools.parser.DetailedLineItemTest.java

private DetailedLineItem givenDetailedLineItemWithoutTags() {
    try {/*from w  ww. j  a v  a 2  s  .c  o  m*/
        CSVParser csvParser = CSVFormat.EXCEL.parse(new StringReader(TEST_RECORD_WITHOUT_TAGS));
        CSVRecord csvRecord = csvParser.getRecords().get(0);
        return givenDetailedLineItem(csvRecord, new ArrayList<>(0));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:javalibs.CSVExtractor.java

private void readCSV() {
    try {/*from   www .  ja va2  s.co m*/
        CSVParser parser = new CSVParser(Files.newBufferedReader(Paths.get(this.inCSV)),
                CSVFormat.DEFAULT.withHeader().withIgnoreHeaderCase().withTrim());

        // Get all headers
        Map<String, Integer> rawHeaders = parser.getHeaderMap();

        // Store the inRecords
        this.inRecords = parser.getRecords();
        parser.close();

        orderHeaders(rawHeaders);
    } catch (IOException e) {
        log_.die(e);
    }
}

From source file:com.xoriant.akka.mongodb.bulkimport.actor.FileReaderActor.java

private void readAndInsertCSV(String filePath) {
    FileReader fileReader = null;

    CSVParser csvFileParser = null;

    // Create the CSVFormat object with the header mapping
    CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader(FILE_HEADER_MAPPING);

    try {/*ww  w . j a  v  a  2  s  . com*/

        fileReader = new FileReader(filePath);

        csvFileParser = new CSVParser(fileReader, csvFileFormat);

        List<CSVRecord> csvRecords = csvFileParser.getRecords();
        CSVRecordBatchMsg csvRecordBatch = new CSVRecordBatchMsg();
        boolean batchSent = false;
        // Skip the header row and start reading CSV records
        for (int i = 1; i < csvRecords.size(); i++) {
            CSVRecord record = csvRecords.get(i);
            BasicDBObject person = new BasicDBObject();
            person.put(PERSON_GENDER, record.get(PERSON_GENDER));
            person.put(PERSON_TITLE, record.get(PERSON_TITLE));
            person.put(PERSON_NAMESET, record.get(PERSON_NAMESET));
            person.put(PERSON_SURNAME, record.get(PERSON_SURNAME));
            person.put(PERSON_CITY, record.get(PERSON_CITY));
            person.put(PERSON_STATE, record.get(PERSON_STATE));
            person.put(PERSON_ZIPCODE, record.get(PERSON_ZIPCODE));
            csvRecordBatch.add(person);
            batchSent = false;
            if (i % batchSize == 0) {
                batchSentCounter++;
                csvRecordBatch.setBatchNo(batchSentCounter);
                mongoInsertionActor.tell(csvRecordBatch, getSelf());
                csvRecordBatch = new CSVRecordBatchMsg();
                batchSent = true;
            }

        }

        // Last batch maybe pending if there are less than batch size left over records. Sending last batch of such records explicitly
        if (!batchSent) {
            batchSentCounter++;
            csvRecordBatch.setBatchNo(batchSentCounter);
            mongoInsertionActor.tell(csvRecordBatch, getSelf());
        }
        mongoInsertionActor.tell(new EndOfFileMsg(), getSelf());
        System.out.println("FileReaderActor: EOF sent");

    } catch (Exception e) {
        System.out.println("Error in CsvFileReader !!!" + e.getMessage());
    } finally {
        try {
            fileReader.close();
            csvFileParser.close();
        } catch (IOException e) {
            System.out.println("Error while closing fileReader/csvFileParser : " + e.getMessage());
        }
    }

}

From source file:com.raceup.fsae.test.TesterGui.java

/**
 * Parses data file, builds a Test/*from w  w  w .  j  ava2 s . co m*/
 * @param pathToDataFile path to data csv file
 */
private void parseDataFileAndCreateTestOrFail(String pathToDataFile) {
    ArrayList<Question> questions = new ArrayList<>();
    CSVRecord[] rows = null;

    try {
        CSVParser parser = CSVFormat.DEFAULT.parse(new FileReader(pathToDataFile));
        rows = parser.getRecords().toArray(new CSVRecord[parser.getRecords().size()]);
    } catch (Exception e) {
        System.err.println(e.toString());
    }

    for (CSVRecord row : rows) { // each row represent a question
        ArrayList<Answer> answers = new ArrayList<>(); // list of answers
        if (row.size() > 1) {
            for (int i = 1; i < row.size(); i++) {
                if (row.get(i).length() > 0) {
                    answers.add(new Answer(row.get(i)));
                }
            }

            Answer correctAnswer = answers.get(0); // the correct
            // answer is always the first one
            String questionText = row.get(0);
            questions.add(
                    new Question(questionText, answers.toArray(new Answer[answers.size()]), correctAnswer)); // add to list of questions
        }
    }
    test = new Test(questions.toArray(new Question[questions.size()]));
}

From source file:apiconnector.TestDataFunctionality.java

@Ignore
@Test//  w  w w  .j  a v a2  s.  com
public void testGetDataAsCsv() throws Exception {
    //client_read.setVerboseLevel(1);
    Random random = new Random();

    Map<String, String> filters = new TreeMap<String, String>();
    filters.put("tag", "study_14");

    DataSet[] all = client_read.dataList(filters).getData();

    for (int i = 0; i < 5;) {
        DataSet current = all[random.nextInt(all.length)];

        String numInst = current.getQualityMap().get("NumberOfInstances");

        if (current.getFileId() == null || !current.getFormat().toLowerCase().equals("arff")) {
            continue;
        }

        String fullUrl = url + "data/get_csv/" + current.getFileId() + "/" + current.getName() + ".csv";
        System.out.println(fullUrl);
        final URL url = new URL(fullUrl);
        final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
        final CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT);
        try {
            if (numInst != null) {
                int numberOfInstances = (int) Double.parseDouble(numInst);
                assertEquals(parser.getRecords().size(), numberOfInstances);
            }
        } finally {
            parser.close();
            reader.close();
        }

        // important
        i += 1;
    }

}

From source file:com.compomics.cell_coord.parser.impl.TSVFileParser.java

@Override
public Sample parseTrackFile(File trackFile) throws FileParserException {
    // create a new sample object -- watch out to set the relationships!
    Sample sample = new Sample(trackFile.getName());
    // initialize an empty list of tracks
    List<Track> list = new ArrayList<>();
    CSVParser tsvFileParser;
    FileReader fileReader;/*from   ww  w .  j  a  v a 2  s . co  m*/
    CSVFormat csvFileFormat = CSVFormat.TDF.withHeader(FILE_HEADER_MAPPING);
    try {
        // initialize the file reader
        fileReader = new FileReader(trackFile);
        //initialize CSVParser object
        tsvFileParser = new CSVParser(fileReader, csvFileFormat);
        // get the csv records
        List<CSVRecord> csvRecords = tsvFileParser.getRecords();
        Track currentTrack = null;
        List<TrackSpot> currentTrackPointList = new ArrayList<>();
        Long currentId = 0L;

        //Read the CSV file records starting from the second record to skip the header
        for (int i = 1; i < csvRecords.size(); i++) {
            CSVRecord cSVRecord = csvRecords.get(i);
            // get the fields
            Long trackid = Long.parseLong(cSVRecord.get(TRACK_ID));
            if (!Objects.equals(currentId, trackid)) {
                currentTrack = new Track();
                currentTrack.setTrackid(trackid);
                list.add(currentTrack);
                currentId = trackid;
                currentTrackPointList = new ArrayList<>();
            }
            // create new Track Spot object
            Long spotid = Long.parseLong(cSVRecord.get(SPOT_ID));
            double x = Double.parseDouble(cSVRecord.get(X_COORD));
            double y = Double.parseDouble(cSVRecord.get(Y_COORD));
            double time = Double.parseDouble(cSVRecord.get(TIME));
            TrackSpot trackSpot = new TrackSpot(spotid, x, y, time, currentTrack);
            currentTrackPointList.add(trackSpot);
            currentTrack.setTrackSpots(currentTrackPointList);
            currentTrack.setSample(sample);
        }
    } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
    } catch (NumberFormatException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new FileParserException(
                "It seems like a line does not contain a number!\nPlease check your files!");
    }
    sample.setTracks(list);
    return sample;
}

From source file:com.compomics.cell_coord.parser.impl.CSVFileParser.java

@Override
public Sample parseTrackFile(File trackFile) throws FileParserException {
    // create a new sample object -- watch out to set the relationships!
    Sample sample = new Sample(trackFile.getName());
    // initialize an empty list of tracks
    List<Track> list = new ArrayList<>();
    CSVParser csvFileParser;
    FileReader fileReader;/*from w ww.j  a  v  a 2s  . c o  m*/
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING);
    try {
        // initialize the file reader
        fileReader = new FileReader(trackFile);
        //initialize CSVParser object
        csvFileParser = new CSVParser(fileReader, csvFileFormat);
        // get the csv records
        List<CSVRecord> csvRecords = csvFileParser.getRecords();
        Track currentTrack = null;
        List<TrackSpot> currentTrackPointList = new ArrayList<>();
        Long currentId = 0L;
        // read the CSV file records starting from the second record to skip the header
        for (int i = 1; i < csvRecords.size(); i++) {
            CSVRecord cSVRecord = csvRecords.get(i);
            // get the fields
            Long trackid = Long.parseLong(cSVRecord.get(TRACK_ID));
            if (!Objects.equals(currentId, trackid)) {
                currentTrack = new Track();
                currentTrack.setTrackid(trackid);
                list.add(currentTrack);
                currentId = trackid;
                currentTrackPointList = new ArrayList<>();
            }
            // create new Track Spot object
            Long spotid = Long.parseLong(cSVRecord.get(SPOT_ID));
            double x = Double.parseDouble(cSVRecord.get(X_COORD));
            double y = Double.parseDouble(cSVRecord.get(Y_COORD));
            double time = Double.parseDouble(cSVRecord.get(TIME));
            TrackSpot trackSpot = new TrackSpot(spotid, x, y, time, currentTrack);
            currentTrackPointList.add(trackSpot);
            currentTrack.setTrackSpots(currentTrackPointList);
            currentTrack.setSample(sample);
        }
    } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
    } catch (NumberFormatException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new FileParserException(
                "It seems like a line does not contain a number!\nPlease check your files!");
    }
    // set the tracks for the sample
    sample.setTracks(list);
    return sample;
}

From source file:convertCSV.ConvertCSV.java

/**
 * @param fichier_//from w w w . ja  v a  2s  .  c  o  m
 * @throws java.io.IOException
 */
public void importer(String fichier_) throws IOException {
    // TODO code application logic here

    float lat, lon, ele, secjour;
    int bpm;
    List<MonPoint> points = new ArrayList<>();
    Reader in = null;
    CSVParser parser;
    List<CSVRecord> list;
    GPXWriter monGPX = new GPXWriter();

    // lecture du CSV
    try {

        System.out.println("Lecture de " + fichier_);
        in = new FileReader(fichier_);

    } catch (FileNotFoundException ex) {
        Logger.getLogger(ConvertCSV.class.getName()).log(Level.SEVERE, null, ex);
    }

    parser = new CSVParser(in, CSVFormat.EXCEL);
    list = parser.getRecords();
    list.remove(0);

    // remplissage de la liste de point GPX
    if (in != null) {
        for (CSVRecord elem : list) {

            try {

                // on recupere les donnees dans le CSV
                lat = Float.parseFloat(elem.get(0));
                lon = Float.parseFloat(elem.get(1));
                ele = Float.parseFloat(elem.get(2));
                secjour = Float.parseFloat(elem.get(3));
                if (elem.size() > 4) {
                    bpm = Integer.parseInt(elem.get(4));
                    points.add(new MonPoint(lat, lon, ele, secjour, bpm));
                } else {
                    points.add(new MonPoint(lat, lon, ele, secjour));
                }

            } catch (NumberFormatException ex) {
                System.out.println(elem.toString());
            }

        }

        // ecriture du GPX
        monGPX.writePath("C:\\Users\\vincent\\Desktop\\today.gpx", "Training", points);
        in.close();

    }

    System.exit(0);
}

From source file:GUI.ReadFile.java

public List<Phase> readPhase(String fileName) {
    FileReader fileReader;/* w  w w .  j  a  v a  2s .  c  o m*/
    CSVParser csvFileParser;
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(PHASE_HEADER_MAPPING);
    List<Phase> phase_list = new ArrayList<>();
    try {
        fileReader = new FileReader(fileName);
        //initialize CSVParser object
        csvFileParser = new CSVParser(fileReader, csvFileFormat);
        //Get a list of CSV file records
        List<CSVRecord> csvRecords = csvFileParser.getRecords();
        //Read the CSV file records starting from the second record to skip the header
        for (int i = 1; i < csvRecords.size(); i++) {
            CSVRecord record = csvRecords.get(i);
            Phase p = new Phase(record.get(st_time), record.get(end_time), record.get(category));
            phase_list.add(p);
        }
        fileReader.close();
        csvFileParser.close();
        System.out.println(fileName + " Phase file read!");
    } catch (FileNotFoundException e) {
        System.out.println(fileName + " Phase file missing ...");
        return null;
    } catch (IOException ex) {
        System.out.println(fileName + " csv file error !!!");
        return null;
    } catch (ParseException ex) {
        System.out.println(fileName + " phase parsing error !!!");
        return null;
    }
    return phase_list;
}