Example usage for org.apache.commons.csv CSVRecord get

List of usage examples for org.apache.commons.csv CSVRecord get

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVRecord get.

Prototype

public String get(final String name) 

Source Link

Document

Returns a value by name.

Usage

From source file:com.linkedin.pinot.core.data.readers.CSVRecordReader.java

private String getValueForColumn(CSVRecord record, String column) {
    if ((_config != null) && (_config.columnIsDate(column))) {
        return dateToDaysSinceEpochMilli(record.get(column)).toString();
    } else {//from   w  w  w. ja v  a  2 s  .com
        return record.get(column);
    }
}

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

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

    CSVParser csvFileParser = null;/*from   w  w w.j av  a 2 s  .  co  m*/

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

    try {

        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:it.vige.greenarea.file.ImportaCSVFile.java

private void aggiungiCampiARichiestaXML(RichiestaXML richiestaXML, CSVRecord cell) throws Exception {
    richiestaXML.setShipmentId(cell.get(0));
    richiestaXML.setIdStop(cell.get(1));
    richiestaXML.setDepot(cell.get(2));/* www . j  a  v  a 2  s .c  o  m*/
    richiestaXML.setPieces(new Integer(cell.get(3)));
    richiestaXML.setWeight(new Double(splitPoint(cell.get(4).replace(",", "."))));
    richiestaXML.setVolume(new Double(splitPoint(cell.get(5).replace(",", "."))));
    long data = new Long(cell.get(6));
    if (date != null)
        data = new Long(dateFormat.format(date.getTime()));
    Date dataFormattata = dateFormat.parse(data + "");
    richiestaXML.setDataEarlestPu(dataFormattata);
    data = new Integer(cell.get(7));
    if (date != null)
        data = new Long(dateFormat.format(date.getTime()));
    dataFormattata = dateFormat.parse(data + "");
    richiestaXML.setDataLatestPu(dataFormattata);
    data = new Integer(cell.get(8));
    if (date != null)
        data = new Long(dateFormat.format(date.getTime()));
    dataFormattata = dateFormat.parse(data + "");
    richiestaXML.setDataEarlestDelivery(dataFormattata);
    data = new Integer(cell.get(9));
    if (date != null)
        data = new Long(dateFormat.format(date.getTime()));
    dataFormattata = dateFormat.parse(data + "");
    richiestaXML.setDataLatestDelivery(dataFormattata);
    richiestaXML.setTimeFromPu(new Double(cell.get(10).substring(0, 5)));
    richiestaXML.setTimeToPu(new Double(cell.get(11).substring(0, 5)));
    richiestaXML.setTimeFromDelivery(new Double(cell.get(12).substring(0, 5)));
    richiestaXML.setTimeToDelivery(new Double(cell.get(13).substring(0, 5)));
    richiestaXML.setHandlingType(cell.get(14));
    String packageType = cell.get(15);
    if (packageType != null)
        richiestaXML.setPackageType(packageType != null ? packageType.charAt(0) : null);
    richiestaXML.setCustomer(cell.get(16));
    richiestaXML.setAddress(cell.get(17));
    String zipCode = cell.get(18);
    richiestaXML.setZipCode(zipCode != null ? new Integer(zipCode) : null);
    richiestaXML.setProvince(cell.get(19));
    richiestaXML.setCity(cell.get(20));
    richiestaXML.setCountry(cell.get(21));
    richiestaXML.setShoppingCentre(new Integer(cell.get(22)));
    richiestaXML.setRoundCode(cell.get(23));
    richiestaXML.setTntType(cell.get(24));
    richiestaXML.setFlagInternational(cell.get(25));
    richiestaXML.setDomesticLdv(cell.get(26));
    richiestaXML.setTaskField(cell.get(27));
    richiestaXML.setTel(cell.get(28));
    richiestaXML.setEmail(cell.get(29));
    richiestaXML.setHandlingClass(new Integer(cell.get(30)));
    richiestaXML.setLocExtId(cell.get(31));
    richiestaXML.setStatus(new Integer(cell.get(32)));
    String latitude = splitPoint(cell.get(33).replace(",", "."));
    String longitude = splitPoint(cell.get(34).replace(",", "."));
    richiestaXML.setLatitude(new Double(latitude));
    richiestaXML.setLongitude(new Double(longitude));
}

From source file:io.mindmaps.migration.csv.CSVDataMigrator.java

/**
 * Migrate all the resources of one Var instance.
 * @param instance var instance the resources belong to
 * @param record the CSVRecord (row) to migrate
 * @return collection of var patterns representing the migrated resources
 *//*from   w  w w .  j av a 2s .  c  o m*/
private Collection<Var> migrateResources(Var instance, CSVRecord record) {
    List<Var> vars = new ArrayList<>();
    for (String resourceType : headers.keySet()) {
        String resourceValue = record.get(resourceType);

        vars.addAll(migrateAsResource(instance, resourceType, resourceValue));
    }
    return vars;
}

From source file:com.publictransitanalytics.scoregenerator.datalayer.directories.GTFSReadingStopTimesDirectory.java

private void parseStopTimesFile(final ImmutableSetMultimap<String, FrequencyRecord> frequencyRecordMap,
        final Reader stopTimesReader) throws FileNotFoundException, IOException, InterruptedException {

    final CSVParser parser = new CSVParser(stopTimesReader, CSVFormat.DEFAULT.withHeader());

    final SortedSetMultimap<String, RawTripStop> rawTripMap = TreeMultimap.create(Comparator.naturalOrder(),
            (stop1, stop2) -> Integer.compare(stop1.getSequence(), stop2.getSequence()));

    final Iterator<CSVRecord> stopTimesIter = parser.iterator();
    while (stopTimesIter.hasNext()) {
        final CSVRecord record = stopTimesIter.next();
        final String rawTripId = record.get("trip_id");
        final int stopSequence = Integer.valueOf(record.get("stop_sequence"));
        final String stopId = record.get("stop_id");
        final String arrivalTimeString = record.get("arrival_time");
        final TransitTime arrivalTime = (arrivalTimeString == null) ? null
                : TransitTime.parse(arrivalTimeString);
        final String departureTimeString = record.get("departure_time");
        final TransitTime departureTime = (departureTimeString == null) ? null
                : TransitTime.parse(arrivalTimeString);

        if (frequencyRecordMap.containsKey(rawTripId)) {
            final RawTripStop rawTripStop = new RawTripStop(arrivalTime, departureTime, stopId, rawTripId,
                    stopSequence);//ww  w .j  av a2  s .  com
            rawTripMap.put(rawTripId, rawTripStop);
        } else {
            final TripId tripId = new TripId(rawTripId);
            final TripStop tripStop = new TripStop(arrivalTime, stopId, tripId, stopSequence);
            try {
                final TripIdKey tripIdKey = new TripIdKey(rawTripId);
                tripsStore.put(tripIdKey, tripId);
                tripSequenceStore.put(new TripSequenceKey(tripIdKey, arrivalTime, stopSequence), tripStop);
                stopTimesStore.put(StopTimeKey.getWriteKey(stopId, arrivalTime), tripStop);
            } catch (final BitvantageStoreException e) {
                throw new ScoreGeneratorFatalException(e);
            }
        }
    }
    for (final String rawTripId : rawTripMap.keySet()) {
        final ImmutableSet<FrequencyRecord> frequencyRecords = frequencyRecordMap.get(rawTripId);
        for (final FrequencyRecord frequencyRecord : frequencyRecords) {

            TransitTime recurringTime = frequencyRecord.getStartTime();
            while (recurringTime.isBefore(frequencyRecord.getEndTime())) {
                final TransitTime baseArrivalTime = rawTripMap.get(rawTripId).first().getArrivalTime();
                final TripId tripId = new TripId(rawTripId, recurringTime.toString());

                for (final RawTripStop rawTripStop : rawTripMap.get(rawTripId)) {
                    final TransitTime arrivalTime = recurringTime
                            .plus(TransitTime.durationBetween(baseArrivalTime, rawTripStop.getArrivalTime()));
                    final int stopSequence = rawTripStop.getSequence();
                    final String stopId = rawTripStop.getStopId();

                    final TripStop tripStop = new TripStop(arrivalTime, stopId, tripId, stopSequence);

                    final TripIdKey tripIdKey = new TripIdKey(tripId.getRawTripId(), tripId.getQualifier());

                    try {
                        tripsStore.put(tripIdKey, tripId);
                        tripSequenceStore.put(new TripSequenceKey(tripIdKey, arrivalTime, stopSequence),
                                tripStop);
                        stopTimesStore.put(StopTimeKey.getWriteKey(stopId, arrivalTime), tripStop);
                    } catch (final BitvantageStoreException e) {
                        throw new ScoreGeneratorFatalException(e);
                    }
                }
                recurringTime = recurringTime.plus(frequencyRecord.getInterval());
            }
        }
    }
}

From source file:MasterRoomControllerFx.rooms.charts.RoomChartController.java

public void getTempData() {
    try {/* w w  w.ja  va  2 s  .c  o m*/
        File csvData = new File("tempHistory" + roomRow + roomColumn + ".csv");
        if (csvData.exists()) {
            CSVParser parser = CSVParser.parse(csvData, StandardCharsets.UTF_8,
                    CSVFormat.EXCEL.withDelimiter(';'));
            for (CSVRecord csvRecord : parser) {
                for (int i = 0; i < csvRecord.size(); i++) {
                    if (i == 0)
                        temp.add(Float.parseFloat(csvRecord.get(i)));
                    if (i == 1)
                        time.add(csvRecord.get(i));
                }
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(RoomChartController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:functions.LoadCSVdata.java

public void LoadFeeDataToJTable(JTable t, String path) {
    try {//from   w ww .ja  v  a2s  .c o m
        file = new File(path);
        parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT);
        DefaultTableModel model = (DefaultTableModel) t.getModel();
        model.setRowCount(0);
        for (CSVRecord c : parser) {
            if (c.getRecordNumber() == 1)
                continue;
            model.addRow(
                    new Object[] { c.get(datatype.GlobalVariable.TYPE), c.get(datatype.GlobalVariable.AMOUNT),
                            c.get(datatype.GlobalVariable.PAID_BY), c.get(datatype.GlobalVariable.PAYER) });
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:edu.emory.mathcs.nlp.zzz.CSVRadiology.java

public void categorize(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    List<CSVRecord> records = parser.getRecords();
    StringJoiner join;/*from   www  . j a va 2s  .co m*/
    CSVRecord record;

    for (int i = 0; i <= 500; i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        join = new StringJoiner(" ");

        for (int j = 2; j < 7; j++)
            join.add(record.get(j));

        System.out.println(join.toString());
    }

    parser.close();
}

From source file:edu.emory.mathcs.nlp.zzz.CSVSentiment.java

public void categorize(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    List<CSVRecord> records = parser.getRecords();
    List<NLPNode[]> document;
    String outputDir;// w w  w.  ja va 2 s  . co  m
    PrintStream fout;
    CSVRecord record;

    System.out.println(inputFile);

    for (int i = 0; i < records.size(); i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        document = decode.decodeDocument(record.get(6));
        document.get(0)[1].putFeat("sent", record.get(0));

        outputDir = inputFile.substring(0, inputFile.length() - 4);
        fout = IOUtils.createBufferedPrintStream(
                outputDir + "/" + FileUtils.getBaseName(outputDir) + "_" + i + ".nlp");
        for (NLPNode[] nodes : document)
            fout.println(decode.toString(nodes) + "\n");
        fout.close();
    }

    parser.close();
}

From source file:edu.emory.mathcs.nlp.zzz.CSVSentiment.java

public void toTSV(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    PrintStream fout = IOUtils.createBufferedPrintStream(inputFile + ".tsv");
    List<CSVRecord> records = parser.getRecords();
    List<Token> tokens;/* w w w  . j  a  va  2 s  . com*/
    CSVRecord record;
    int label;

    System.out.println(inputFile);

    for (int i = 0; i < records.size(); i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        label = toIntLabel(record.get(0));
        tokens = tokenizer.tokenize(record.get(6));
        fout.println(label + "\t" + Joiner.join(tokens, " ", Token::getWordForm));
    }

    fout.close();
    parser.close();
}