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.github.jferard.pgloaderutils.sniffer.csd.CSDValidatorHelper.java

/**
 * @param result the result that will hold the errors
 * @param fields the schema / schema pattern
 * @param record the current record of the stream
 * @param line the current line number/* w ww . java2s .  co m*/
 * @return the error count, -1 the schema has too many fields
 */
public int validateRecord(CSDValidationResult<F> result, SizedIterable<F> fields, CSVRecord record, int line) {
    if (record.size() < fields.size()) {
        result.schemaHasTooManyFieldsForRecord(line, record);
        return -1;
    }

    int errorCount = 0;
    int j = 0;
    for (F field : fields) {
        String value = record.get(j++);
        if (!field.validate(value)) {
            result.incorrectValue(line, value, field);
            errorCount++;
        }
    }
    return errorCount;
}

From source file:com.streamsets.pipeline.lib.csv.CsvParser.java

private String[] toArray(CSVRecord record) {
    String[] array = (record == null) ? null : new String[record.size()];
    if (array != null) {
        for (int i = 0; i < record.size(); i++) {
            array[i] = record.get(i);
        }//from   w w  w.j a  va  2s .c om
    }
    return array;
}

From source file:net.sourceforge.ganttproject.io.CsvImportTest.java

public void testSkipLinesWithEmptyMandatoryFields() throws IOException {
    String header = "A, B, C";
    String data1 = "a1,,c1";
    String data2 = "a2,b2,c2";
    String data3 = ",b3,c3";
    final AtomicBoolean wasCalled = new AtomicBoolean(false);
    GanttCSVOpen.RecordGroup recordGroup = new GanttCSVOpen.RecordGroup("ABC",
            ImmutableSet.<String>of("A", "B", "C"), ImmutableSet.<String>of("A", "B")) {
        @Override//from www  .j a v a 2s  .  c om
        protected boolean doProcess(CSVRecord record) {
            if (!hasMandatoryFields(record)) {
                return false;
            }
            wasCalled.set(true);
            assertEquals("a2", record.get("A"));
            assertEquals("b2", record.get("B"));
            return true;
        }
    };
    GanttCSVOpen importer = new GanttCSVOpen(createSupplier(Joiner.on('\n').join(header, data1, data2, data3)),
            recordGroup);
    importer.load();
    assertTrue(wasCalled.get());
    assertEquals(2, importer.getSkippedLineCount());
}

From source file:com.github.jferard.pgloaderutils.sniffer.csd.CSDValidatorHelper.java

/**
 * @param result the result that will hold the errors
 * @param fields the schema / schema pattern
 * @param firstRecord the first record of the stream
 * @return the error count, -1 the schema has too many fields
 *///from   w ww  .j av  a  2 s.co m
public int validateHeader(CSDValidationResult<F> result, SizedIterable<F> fields, CSVRecord firstRecord) {
    int headerErrorCount = 0;
    if (firstRecord.size() < fields.size()) {
        result.schemaHasTooManyFieldsForHeader(firstRecord);
        return -1;
    }

    int j = 0;
    for (F field : fields) {
        String value = firstRecord.get(j++);
        if (!matcher.match(field, value)) {
            result.incorrectColumnName(field, value);
            headerErrorCount++;
        }
    }
    return headerErrorCount;
}

From source file:com.objy.se.utils.TargetList.java

private void addToTargetInfoMap(CSVRecord record, SingleKey... singleKeywords) {
    Property[] nameValues = new Property[singleKeywords.length];
    for (int i = 0; i < singleKeywords.length; i++) {
        String value = record.get(singleKeywords[i].rawFileAttrName);
        Object var = targetClass.getCorrectValue(value, singleKeywords[i].logicalType);
        nameValues[i] = new Property(singleKeywords[i].attrName, var);
    }//from w w  w . j  a  v a  2 s  .  c om
    addToTargetInfoMap(nameValues);
}

From source file:com.team3637.service.TeamServiceMySQLImpl.java

@Override
public void importCSV(String inputFile) {
    try {/*from  w w  w.j  av  a 2 s. com*/
        String csvData = new String(Files.readAllBytes(FileSystems.getDefault().getPath(inputFile)));
        csvData = csvData.replaceAll("\\r", "");
        CSVParser parser = CSVParser.parse(csvData, CSVFormat.DEFAULT.withRecordSeparator("\n"));
        for (CSVRecord record : parser) {
            Team team = new Team();
            team.setId(Integer.parseInt(record.get(0)));
            team.setTeam(Integer.parseInt(record.get(1)));
            team.setAvgscore(Double.parseDouble(record.get(2)));
            team.setMatches(Integer.parseInt(record.get(3)));
            String[] tags = record.get(4).substring(1, record.get(4).length() - 1).split(",");
            for (int i = 0; i < tags.length; i++)
                tags[i] = tags[i].trim();
            if (tags.length > 0 && !tags[0].equals(""))
                team.setTags(Arrays.asList(tags));
            else
                team.setTags(new ArrayList<String>());
            if (checkForTeam(team.getTeam()))
                update(team);
            else
                create(team);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:biz.ganttproject.impex.csv.CsvImportTest.java

public void testTwoGroups() throws Exception {
    String header1 = "A, B";
    String data1 = "a1, b1";
    final AtomicBoolean wasCalled1 = new AtomicBoolean(false);
    RecordGroup recordGroup1 = new RecordGroup("AB", ImmutableSet.<String>of("A", "B")) {
        @Override//from  w ww . j  ava 2s .c o  m
        protected boolean doProcess(CSVRecord record) {
            if (!super.doProcess(record)) {
                return false;
            }
            assertEquals("a1", record.get("A"));
            assertEquals("b1", record.get("B"));
            wasCalled1.set(true);
            return true;
        }
    };

    String header2 = "C, D, E";
    String data2 = "c1, d1, e1";
    final AtomicBoolean wasCalled2 = new AtomicBoolean(false);
    RecordGroup recordGroup2 = new RecordGroup("CDE", ImmutableSet.<String>of("C", "D", "E")) {
        @Override
        protected boolean doProcess(CSVRecord record) {
            if (!super.doProcess(record)) {
                return false;
            }
            assertEquals("c1", record.get("C"));
            assertEquals("d1", record.get("D"));
            assertEquals("e1", record.get("E"));
            wasCalled2.set(true);
            return true;
        }
    };
    GanttCSVOpen importer = new GanttCSVOpen(
            createSupplier(Joiner.on('\n').join(header1, data1, "", header2, data2)), recordGroup1,
            recordGroup2);
    importer.load();
    assertTrue(wasCalled1.get() && wasCalled2.get());
}

From source file:com.team3637.service.MatchServiceMySQLImpl.java

@Override
public void importCSV(String inputFile) {
    try {//from   w  ww  . j  a v  a 2s. com
        String csvData = new String(Files.readAllBytes(FileSystems.getDefault().getPath(inputFile)));
        csvData = csvData.replaceAll("\\r", "");
        CSVParser parser = CSVParser.parse(csvData, CSVFormat.DEFAULT.withRecordSeparator("\n"));
        for (CSVRecord record : parser) {
            Match match = new Match();
            match.setId(Integer.parseInt(record.get(0)));
            match.setMatchNum(Integer.parseInt(record.get(1)));
            match.setTeam(Integer.parseInt(record.get(2)));
            match.setScore(Integer.parseInt(record.get(3)));
            String[] tags = record.get(4).substring(1, record.get(4).length() - 1).split(",");
            for (int i = 0; i < tags.length; i++)
                tags[i] = tags[i].trim();
            if (tags.length > 0 && !tags[0].equals(""))
                match.setTags(Arrays.asList(tags));
            else
                match.setTags(new ArrayList<String>());
            if (checkForMatch(match.getMatchNum(), match.getTeam()))
                update(match);
            else
                create(match);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:edu.uiowa.icts.util.SummarizeListTest.java

@Test
public void getSummeryFromCSVFile() throws IOException {
    File file = new File("src/test/resources/ClinicalData-BLCA-table.csv");
    assertTrue(file.isFile());/*from  w w  w  . ja  v  a 2  s  . c  o m*/
    Reader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    Iterable<CSVRecord> records = CSVFormat.DEFAULT
            .withHeader("barcode", "Clinical:days_to_death", "Clinical:vital_status",
                    "Clinical:anatomic_treatment_site", "Clinical:bcr_aliquot_barcode",
                    "Clinical:bcr_radiation_barcode", "Clinical:bcr_radiation_uuid", "Clinical:analyte_type")
            .withSkipHeaderRecord(true).parse(in);

    List<String> values = new ArrayList<String>();
    List<String> barcodeValues = new ArrayList<String>();
    List<String> vitalStatus = new ArrayList<String>();

    for (CSVRecord record : records) {
        values.add(record.get("Clinical:bcr_aliquot_barcode"));
        barcodeValues.add(record.get("barcode"));
        vitalStatus.add(record.get("Clinical:vital_status"));
    }
    SummarizeList sl = new SummarizeList(values, null, 2);
    assertEquals("Identifier: {# of Unique ID's = ".concat(new Integer(values.size()).toString()).concat("}"),
            sl.getSummary());

    sl = new SummarizeList(barcodeValues, null, 2);
    assertEquals(
            "Identifier: {# of Unique ID's = ".concat(new Integer(barcodeValues.size()).toString()).concat("}"),
            sl.getSummary());

    sl = new SummarizeList(vitalStatus, null, 2);
    assertEquals("Categorical: {LIVING=94, DECEASED=34}", sl.getSummary());
}

From source file:edu.washington.gs.skyline.model.quantification.QuantificationTest.java

private Map<RecordKey, Double> readExpectedRows(String filename) throws Exception {
    Map<RecordKey, Double> map = new HashMap<>();
    Reader reader = new InputStreamReader(QuantificationTest.class.getResourceAsStream(filename));
    try {//from ww w  .j a va  2 s. c  om
        CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
        for (CSVRecord record : parser.getRecords()) {
            map.put(new RecordKey(record), parseNullableDouble(record.get("NormalizedArea")));
        }
    } finally {
        reader.close();
    }
    return map;
}